JavaScript ES6 - Tuong-Nguyen/JavaScript-Structure GitHub Wiki

New Scope

let - const vs var Block scope {} vs Function scope & hoisting

(function () {
    console.log(block);

    if(true){
        let block = "block";
    }
})();
  • block variable is defined in the if block
  • block variable can be used after its declaration.

Default variable

function logSomething(something = 'default message', somethingElse = something + ' with something') {
    console.log(something, somethingElse);
}

// do not pass argument - default values are used.
logSomething();
logSomething('hello', ' and overriding');

Rest variable

Put arguments into an array

    function join(...items) {
        return items.join(' ');
    }

    console.log(join(1 , 2, 3));

Spread operator ...

Break an array into individual elements

    function sum(a, b, c) {
        return a + b + c;
    }

    console.log(sum(...[1, 2, 3]));

Template string

  • Multiple lines
  • Interpolation ${}
  • Tag string: prefix a template string with a function
// multiple line
let multiple = `this is
multiple line string`;

// Interpolate expression into string
let a = 3;
let s = `a = ${a}`;

// Tag string
// stringArrays: array of string separated by interpolations
// values: interpolations values
function str(stringArrays, ...values){
   return values[0] + stringArrays[0];
}

str`a = ${3 + 5}`;

String Object

  • Member: startsWith - endsWith - includes - repeat -
  • Static: String.fromCodePoint

Number Object

Number.EPSILON Number.MAX_SAFE_INTERGER Number.MIN_SAFE_INTERGER

Number.isNan vs. isNan Number.isFinite Number.isNumber Number.isSafeInteger

Array Object

  • Array.of: create array from elements

  • Array.from: create an array from an arraylike object

  • find(callback), findIndex

  • copyWithin(to, from)

Object literal


let x = 2;
let y = 'test';
let myObj = {
   // Define properties
   x, y

   // define method
   someMethod(aParam) {
      console.log(aParam);
   }
   
   // computed propety
   [x + y]: "some value";
};

- Object.assign: copy properties from objects to target object

Set

Collection of unique elements

  • size
  • add: add new elements
  • delete(element)
  • clear: remove all elements
  • has: check if element is in the set
  • foreach(callback)

Map

(Key,Value) collection

  • set: add new (key,value)
  • get
  • has

WeakSet - WeakMap

Iterator

  • done: complete or not
  • value
  • next()

set.entries() set.values()

for of Loop

loop in iterators

for (let val of ['a', 'b', 'c']) {
   consle.log(val);
}

Modules

  • default export is flavored.
  • static module structure.
  • support async/sync loading.
  • support cycling dependencies.
// Default export and named exports
import theDefault, {name1, name2} from '/src/mylib';
import theDefault from '/src/mylib';
import {name1, name2} from '/src/mylib';

// Renaming: import name1 to myName1
import {name1 as myName1, name2} from '/src/mylib';

// Importing the module as an object
// (with one property per named export)
import * as mylib from 'src/mylib';

// Only load the module, do not import anything
import 'src/mylib';
export * from 'src/other_module';

export { foo, bar } from 'src/other_module';

export { foo as myFoo, bar } from 'src/other_module';
  • Programmatic Loading API:
System.import('some_module')
   .then(some_module => {
   ...
   })
   .catch(error => {
   ...
   });

Practices

http://es6katas.org/

Environment setup:

  • babel

  • mocha

  • chai

  • Install packages

npm install babel-core --save-dev
npm install babel-preset-es2015 --save-dev
npm install mocha chai --save-dev
  • Configure .babelrc for ES6
{
  "presets": ["es2015"]
}
  • Set mocha to use babel for ES6 mocha --compilers js:babel-core/register