ES2015 - KeynesYouDigIt/Knowledge GitHub Wiki

Arrow Functions

Instead of:

function(event){
    //
}

Use this to pass the this context in:

event => {
    //
}

shorthand function calls: foo = bar.map(baz => baz + 1)

Assignment

  • let is var, but a block-level scope.
  • const = can't be reassigned

Rest & Spread

function (parameterOne, ...restParameters){
    var spread = ...restParameters; // An array of any other passed in parameters
}

Template Strings

`Here’s a string with a {$variableName} variable in it`

Destructuring

{x, y}; // x=x, y=y

Classes

class Shape {
    constructor (id, x, y){
        this.id = id;
        this.move(x, y);
    }
    move(x, y){
        this.x = x;
        this.y = y;
    }
    set width(width){
        this._width = width;
    }
    get width(){
        return this._width;
    }
}

for of

for (let n of fibonacci) {
    if (n > 1000)
        break;
    console.log(n);
}

Generators

function* range (start, end, step) {
    while (start < end) {
        yield start;
        start += step;
    }
}

String functions

[ 1, 3, 4, 2 ].find(x => x > 3); // 4
"hello".startsWith("ello", 1); // true
"hello".endsWith("hell", 4);  // true
"hello".includes("ell");      // true

Notes

  • Can nest functions now (block scope)
  • Can use default parameters
  • Property names can be computed now
  • export/import modules
  • Can use promises (new Promise(resolve, reject))