Show features of ECMAScript 2015 with examples, most from babeljs.io.
Hints for usage with Node.js, webpack, React and a link to the implementation status in different browsers.
pretty little template string.` // Multiline strings `In ES5 this is not legal.` // Interpolate variable bindings var name = "Bob", time = "today"; `Hello ${name}, how are you ${time}?` // Unescaped template strings String.raw`In ES5 "\n" is a line-feed.`
=== 1 b === 3 // object matching var { op: a, lhs: { op: b }, rhs: c } = getNode() // Fail-soft destructuring var [a] = [] a === undefined // Fail-soft destructuring with defaults var [a = 1] = [] a === 1 var _getASTNode = getNode(); var a = _getASTNode.op; var b = _getASTNode.lhs.op; var c = _getASTNode.rhs; ES 5
'ops') var b1 = _.get(source, 'lhs.op') var b2 = _.get(source, ['lhs', 'op']) var c =_.get(source, 'hrs') _.get(source, 'really.deep[2]["and-dynamic"]')
y is 12 if not passed (or passed as undefined) return x + y; } f(3) == 15 function f(x, ...y) { // y is an Array return x * y.length; } f(3, "hello", true) == 6 function f(x, y, z) { return x + y + z; } // Pass each elem of array as argument f(...[1,2,3]) == 6
// okay, block scoped name const x = "sneaky"; // error, const x = "foo"; } // okay, declared with `let` x = "bar"; // error, already declared in block let x = "inner"; } }
pre = 0, cur = 1; return { next() { [pre, cur] = [cur, pre + cur]; return { done: false, value: cur } } } } } for (var n of fibonacci) { // truncate the sequence at 1000 if (n > 1000) break; console.log(n); }
= 0, cur = 1; for (;;) { var temp = pre; pre = cur; cur += temp; yield cur; } } } for (var n of fibonacci) { // truncate the sequence at 1000 if (n > 1000) break; console.log(n); }
= 0, cur = 1; for (;;) { var temp = pre; pre = cur; cur += temp; yield cur; } } } for (var n of fibonacci) { // truncate the sequence at 1000 if (n > 1000) break; console.log(n); }
{ var pre = 0, cur = 1; for (;;) { var temp = pre; pre = cur; cur += temp; await cur; } } } for (var n of fibonacci) { // truncate the sequence at 1000 if (n > 1000) break; console.log(n); }
s = new Set() s.add("hello").add("goodbye").add("hello") s.size === 2 s.has("hello") === true // Maps var m = new Map() m.set("hello", 42) m.set(s, 34) m.get(s) == 34 // Weak Maps var wm = new WeakMap() wm.set(s, { extra: 42 }) wm.size === undefined // Weak Sets var ws = new WeakSet() ws.add({ data: 42 }) // Because the added object has no other references, // it will not be held in the set
= Symbol() typeof foo === "symbol" typeof bar === "symbol" let obj = {} obj[foo] = "foo" obj[bar] = "bar" JSON.stringify(obj) // {} Object.keys(obj) // [] Object.getOwnPropertyNames(obj) // [] Object.getOwnPropertySymbols(obj) // [ foo, bar ] A symbol is a unique and immutable data type and may be used as an identifier for object properties. The symbol object is an implicit object wrapper for the symbol primitive data type.
= Symbol.for("app.bar") Symbol.keyFor(foo) === "app.foo" Symbol.keyFor(bar) === "app.bar" The Symbol.for(key) method searches for existing symbols in a runtime-wide symbol registry with the given key and returns it if found. Otherwise a new symbol gets created in the global symbol registry with this key.
by default on Node.js and do NOT require any kind of runtime flag. In progress features can be activated individually by their respective harmony flag (e.g. --harmony_destructuring), although this is highly discouraged unless for testing purposes. node --v8-options | grep harmony
P - Proxies X - Symbols P* - Subclassable Built-ins TP* - Math + Number + String + Object APIs P* - Binary and Octal Literals T* - Promises P* - Reflect API P* - Tail Calls T* - Arrows and Lexical This T - Classes T - Enhanced Object Literals T - Template Strings T - Destructuring T - Default + Rest + Spread T - Let + Const T - Iterators + For..Of TP - Generators TP - Unicode T - Modules T T = Transpiler P = Polyfill X = not supported at all * = limited / partial support