JS: ECMAScript 2015

By Xah Lee. Date: . Last updated: .

Over 10 major features are added to the language, effectively making JavaScript a new language.

Here is a quick overview of new features.

Let

let is like var , but with a clean block-level scope.

Const

const is similar to let, except you can't change its value once set.

Arrow function

you can write function like this

((x, y) => { return x + y; });

but no this-binding , and no arguments Object, no Function Name Hoisting .

Function argument default values

function parameter can specify a default value.

// function with default parameter values
function f(x = 4, y = 2) {
 return [x, y];
}
console.log(f());
// [ 4, 2 ]

Rest parameters

function can now specify to take any number of parameters, by syntax ...args

// 2 parameters plus a rest param
function ff(a, b, ...c) {
 return c;
}
console.log(ff(1, 2, 3, 4));
// [ 3, 4 ]

Assignment pattern

// assignment pattern
let [x, y, z] = [3, 4, 6];

Object literal expression, extensions

Computed Property Keys

new syntax for object literal to have computed property key by an expression.

// Object Literal Expression, Computed Property Keys
const expr = "cat";
const xobj = { [expr]: 4 };
console.log(xobj);
// { cat: 4 }

Method Definition Shorthand

new syntax shorthand for defining methods in object literal expression.

// object literal expression method definition shorthand
const jj = {
 xmethod(x) {
  return x + 1;
 },
 gg: 4,
};
console.log(jj.xmethod(2));
// 3

Key without value Shorthand

{ a }

means

{ a: a }

if a is not defined, it's an error.

// key without value shorthand for object literal expression
const dog = 3;
const cat = 4;
const jj = { dog, cat };
console.log(jj);
// { dog: 3, cat: 4 }

Template string

allows you to embed variable or expression in a string, or have newline char.

// template string example
console.log(`2 + 3 is ${2 + 3}`);
// 2 + 3 is 5

Interface, iterable, iterator, generator

Iterable is new. It is a like an delayed array, items are generated at time it is used.

array is made iterable.

iterator is created to support iterable.

for-of loop and spread operator works with iterables.

these are created to support iterable.

Generator Function is created to support creating iterable.

Spread operator

The spread operator is 3 dots:

...xArray

What it does is insert the array items (into a array, or as function args). The operand can be any iterable.

For-of loop

loop thru an iterable.

Set object

Set object is data structure of set. That is, a collection of unique values.

Map object

Map object is a data structure of dictionary. Similar to Data Object, but now designed to be used as a data structure, have length, keys can be any type (not just string), etc.

const xx = new Map();

// add new item
xx.set(1, "n1");
xx.set(2, "n2");

console.log(xx);
// Map { 1 => "n1", 2 => "n2" }

Reflect

The Reflect object is used as namespace to host a group of static functions. Reflect methods typically duplicate operator behavior in function form, or provides a more strict or better version of existing methods.

Class

class is a new keyword that lets you conveniently define object and its prototype.

New keywords are: class, constructor, static, extends, super.

Regex

New regex flag "u". For unicode. Treat string as sequence of unicode characters.

New regex flag "y". For “sticky”. This means next regex match call will start with lastIndex.

New value properties.

New function properties.

New array properties

new array constructor properties

new array prototype properties

New string properties

static methods

string methods

New object methods

New function properties

function property "name"

New number properties

New math methods

Html-like comments

formally recognize html comment.

Block-level function declaration

now allow function declaration inside a block.

// es2015 block-level function declaration

function f() {
  function g(x) {
    return x + 1;
  }
  return g(1);
}

console.log(f()); // 2

Module, keyword “import”, “export”

Unicode escape syntax

Now you can escape unicode by \u{4_to_6_hex_digits}

Octal and binary literals

// octal notation
console.log( 0o10 )
// 8

// binary
console.log( 0b10 )
// 2

New.target

new.target is a special meta-property in JavaScript that tells you whether a function (or class constructor) was invoked using the new keyword.

function ff() {
 if (new.target) console.log("called with new.\n", "new.target value is: ", new.target);
 else console.log("called as function.\n", "new.target value is:", new.target);
}

new ff();
// called with new.
// new.target value is:  [Function: ff]

ff();
// called as function.
// new.target value is: undefined

Promise

Subclassing

Misc

__proto__

document the property key __proto__ to get/set parent. It was from Firefox.

Annex b

Arraybuffer, dataview, typed array

Weakmap, weakset

Proxy

it allows you to create a new object, such that you can intercept any operation with the object.

Tail call optimisation (recursion)

Reference