links: JS MOC


3 Foundational Pillars Of JS

  1. Scope/Closures
  2. Prototypes/Objects
  3. types/Coercion

Origin of JS

  • Netscape called mocha/live script, due to marketing javascript won the vote. there are other names like js, ecma script

Language Spec

  • TC39 is a technical steering committee that manages JS and decides it’s future
  • Any proposal for new feature goes from Stage 0 to Stage 4

The Web rules everything about (JS)

  • If a change to ecma spec breaks web, browser engine can refuse to confirm the dictated change
  • If both TC39 stands firm for spec change and browser engine refuses, the solution is Appendix B where mismatches are listed
  • Section B1 and B2 cover additions of JS that web JS includes, but not formally specify in the Core of JS
  • Section B3 includes conflicts where web and other engines run, but with different outcomes
  • The mismatch is allowed only for web, other engines should comply the spec

Not all (web) JS

  • alert, fetch, getCurrentLocation, getUserMedia, are all web API’s that look like JS
  • Various JS environments add APIs into the global scope of your JS programs, that give you environment specific capabilities
    • alert in web
    • fs.write in Node

It’s not always JS

  • Developer tools are tools for developers, so don’t consider it as Pure JS environment

Many Faces

  • The term “paradigm” in programming language context refers to a broad (almost universal) mindset and approach to structuring code
  • Paradigm level code categories
    • Procedural
    • OO Style
    • FP Style
  • JavaScript is a Multi Paradigm language

Backwards & Forwards

  • JS is backward compatible which means, if a spec is added 25 years ago, it will still work at any time in future
  • HTML, Css are forward compatible, which means old code doesn’t work in new browsers, but new code will work in old browsers(skipped)

Jumping the gaps

  • Since JS is not forward compatible, we can transpile the Code which can run on older JS engines

Filling the gaps

  • if an API is recently added, and not present in old engine. the solution is to provide a definition to that API that stands in and acts as if the older environment had it. This pattern is called polyfill (a.k.a shim)

What’s in an interpretation?

  • JS is more of a compiled language than interpreted because, it checks for syntax errors and runs code

The flow is:

  1. Code gets transpiled by babel, then packed by webpack(and half a dozen build processes), then it gets delivered in that very different form to JS engine
  2. The JS engine parses the code to AST
  3. Then the engine converts the AST to kind of byte code, a binary intermediate representation, which is then refined/converted even further by the optimizing JIT compiler
  4. Finally the JS Vm executes the program

Web Assembly(WASM)

  • Initially mozilla did a experiment called ASM.Js where it can run unity code(C) in browser. Which paved the path to Wasm where you can run other language code in browser
  • Wasm compiles Ahead of Time(AOT) and outputs wasm files
  • These files can be run in browser without parsing and compilation step

Strictly Speaking

  • In ES5 release, Strict mode is introduced as an opt in mechanism for encouraging better Js programs
  • Strict mode can show linting errors and other errors
  • One can turn on strict mode with this line "use strict";
  • Developer should try to write code with strict mode enabled

tags: ydkjs, book