es6 notes

Exploring ES6: Upgrade to the next version of JavaScript – Book by Dr. Axel Rauschmayer

Destructuring – Dr. Axel Rauschmayer (Destructuring was introduced on TypeScript 1.5 and is discussed here.)

From the traceur wiki:

Traceur is a compiler that takes ECMAScript Edition 6 (ES6) (including classes, generators, destructuring and much more) and compiles it down to regular Javascript (ECMAScript Edition 5 [ES5]) that runs in your browser.

Per answer to my stackoverflow question (9.1.2015), Traceur is still needed by angular2 – even if one uses TypeScript to create ES5 code.

Pascal Precht on why a ES6 module loader still required with TypeScript 1.5

Whenever you transpile ES6+ code that uses ES6 modules to ES5, you have to decide for a module system that works in ES5. That can be CommonJS, AMD, SystemJS, UMD. …Even if TypeScript is going to be a superset of ES6 and implement ES6 modules, as soon as you transpile your code, you need to use a module system that the browser understands today. …Unfortunately, there’s no spec that defines how modules are loaded anymore. So yes, you’d still need to use something like SystemJS to load modules in ES5.”

let, const, and var

From JavaScript ES6 Variable Declarations with let and const – 3.17.2015

…I want to identify what some folks are considering the new standard for declaring variables in ES6. The basic idea here is that let should, in time, replace var as the default declaration keyword. In fact, according to some, var should simply not be used at all in new code. The const keyword should be used for any variable where the reference should never be changed, and let for anything else. …const follows the same new scoping rules as let!

  • var creates a variable scoped within its nearest parent function.
  • let scopes the variable to the nearest block, this includes for loops, if statements, and others.
  • const  represents a constant reference to a value (the same is true in most languages, in fact). In other words, the pointer that the variable name is using cannot change in memory, but the thing the variable points to might change.

ES6 Module Loader Polyfill README

The book Exploring es6 has a chapter on modules

Microsoft TypeScript wiki – Modules

so… should we use var, let, and const?

One can see immediately that var and let may be theoretically unnecessary, but in practice make the functions we write simpler, and therefore easier to read and write.

Whereas, const does not make functions simpler than let, but does provide a kind of annotation that saves us some effort when examining a function. It is not nearly as useful as immutable data, because the problem it solves is easy, not hard.