Hemanth's Scribes

node

ES6 on Node.js

Author Photo

Hemanth HM

Thumbnail

Here I demonstrate ES6 (harmony) features using raw Node.js (v0.11.6).

Enabling Harmony Features

$ node --v8-options | grep harm
--harmony_typeof
--harmony_scoping
--harmony_modules
--harmony_symbols
--harmony_proxies
--harmony_collections
--harmony_generators
--harmony_iteration
--harmony_numeric_literals
--harmony_strings
--harmony_arrays
--harmony (enable all harmony features)

Enable all features with strict mode:
bash
$ node --use-strict $(node --v8-options | grep harm | awk '{print $1}' | xargs)

Block Scoping with let

function aboutme() {
  {
    let gfs = 10;
    var wife = 1;
  }
  console.log(wife); // 1
  console.log(gfs);  // ReferenceError: gfs is not defined
}

let defines a variable in the current block, unlike var which is function-scoped.

Generators

Generators help build iterators. yield suspends execution:

function* Counter() {
  var n = 0;
  while (1 < 2) {
    yield n;
    ++n;
  }
}

var CountIter = new Counter();
CountIter.next() // { value: 0, done: false }
CountIter.next() // { value: 1, done: false }

The done attribute will be true once the generator has nothing more to yield.

#node#javascript#es6
Author Photo

About Hemanth HM

Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.