Learn ES2015
REPL
Be sure to try these features out in the online REPL.
ECMAScript 2015 is an ECMAScript standard that was ratified in June 2015.
ES2015 is a significant update to the language, and the first major update to the language since ES5 was standardized in 2009. Implementation of these features in major JavaScript engines is .
See the ES2015 standardfor full specification of the ECMAScript 2015 language.
ECMAScript 2015 Features
### Arrows and Lexical ThisArrows are a function shorthand using the syntax. They are syntacticallysimilar to the related feature in C#, Java 8 and CoffeeScript. They supportboth expression and statement bodies. Unlike functions, arrows share the samelexicalthis
as their surrounding code. If an arrow is inside another function,it shares the "arguments" variable of its parent function.### ClassesES2015 classes are a simple sugar over the prototype-based OO pattern. Having asingle convenient declarative form makes class patterns easier to use, andencourages interoperability. Classes support prototype-based inheritance, supercalls, instance and static methods and constructors.### Enhanced Object LiteralsObject literals are extended to support setting the prototype at construction,shorthand for
class SkinnedMesh extends THREE.Mesh { constructor(geometry, materials) { super(geometry, materials); this.idMatrix = SkinnedMesh.defaultMatrix(); this.bones = []; this.boneMatrices = []; //… } update(camera) { //… super.update(); } static defaultMatrix() { return new THREE.Matrix4(); }}
foo: foo
assignments, defining methods and making super calls.Together, these also bring object literals and class declarations closertogether, and let object-based design benefit from some of the sameconveniences.
var obj = { // Sets the prototype. "proto" or 'proto' would also work. proto: theProtoObj, // Computed property name does not set prototype or trigger early error for // duplicate _proto
properties. ['__proto
']: somethingElse, // Shorthand for ‘handler: handler’ handler, // Methods toString() { // Super calls return "d " + super.toString(); }, // Computed (dynamic) property names [ "prop" + (() => 42)() ]: 42};
### Template StringsTemplate strings provide syntactic sugar for constructing strings. This issimilar to string interpolation features in Perl, Python and more. Optionally, atag can be added to allow the string construction to be customized, avoidinginjection attacks or constructing higher level data structures from stringcontents.The
proto
property requires native support, and was deprecated in previous ECMAScript versions. Most engines now support the property, but . Also, note that only web browsers are required to implement it, as it's in . It is available in Node.
### DestructuringDestructuring allows binding using pattern matching, with support for matchingarrays and objects. Destructuring is fail-soft, similar to standard objectlookup
// Basic literal string creation
This is a pretty little template string.
// Multiline stringsIn ES5 this is
not legal.
// Interpolate variable bindingsvar name = "Bob", time = "today";Hello ${name}, how are you ${time}?
// Unescaped template stringsString.rawIn ES5 "\n" is a line-feed.
// Construct an HTTP request prefix is used to interpret the replacements and constructionGEThttp://foo.org/bar?a=${a}&b=${b}
Content-Type: application/json
X-Credentials: ${credentials}
{ "foo": ${foo},
"bar": ${bar}}
(myOnReadyStateChangeHandler);
foo["bar"]
, producing undefined
values when not found.### Default + Rest + SpreadCallee-evaluated default parameter values. Turn an array into consecutivearguments in a function call. Bind trailing parameters to an array. Restreplaces the need for and addresses common cases more directly.
function f(x, y=12) { // 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
### Let + ConstBlock-scoped binding constructs.
function f(x, y, z) { return x + y + z;}// Pass each elem of array as argumentf(…[1,2,3]) == 6
let
is the new var
. const
issingle-assignment. Static restrictions prevent use before assignment.### Iterators + For..OfIterator objects enable custom iteration like CLR IEnumerable or JavaIterable. Generalize
function f() { { let x; { // this is ok since it's a block scoped name const x = "sneaky"; // error, was just defined with
const
above x = "foo"; } // this is ok since it was declared withlet
x = "bar"; // error, already declared above in this block let x = "inner"; }}
for..in
to custom iterator-based iteration withfor..of
. Don’t require realizing an array, enabling lazy design patterns likeLINQ.Iteration is based on these duck-typed interfaces (using type syntax for exposition only):
let fibonacci = { Symbol.iterator { let 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);}
### GeneratorsGenerators simplify iterator-authoring usingSupport via polyfill
In order to use Iterators you must include the Babel polyfill.
function
and yield
. A functiondeclared as function returns a Generator instance. Generators are subtypes ofiterators which include additional next
and throw
. These enable values toflow back into the generator, so yield
is an expression form which returns avalue (or throws).Note: Can also be used to enable ‘await’-like async programming, see also ES7 await
.The generator interface is (using TypeScript typesyntax for exposition only):
var fibonacci = { [Symbol.iterator]: function
() { var pre = 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);}
interface Generator extends Iterator { next(value?: any): IteratorResult; throw(exception: any);}
### ComprehensionsRemoved in Babel 6.0### UnicodeNon-breaking additions to support full Unicode, including new unicode literalform in strings and new RegExpSupport via polyfill
u
mode to handle code points, as well as newAPIs to process strings at the 21bit code points level. These additions supportbuilding global apps in JavaScript.### ModulesLanguage-level support for modules for component definition. Codifies patternsfrom popular JavaScript module loaders (AMD, CommonJS). Runtime behaviourdefined by a host-defined default loader. Implicitly async model – no codeexecutes until requested modules are available and processed.
// same as ES5.1"𠮷".length == 2// new RegExp behaviour, opt-in ‘u’"𠮷".match(/./u)[0].length == 2// new form"\u{20BB7}" == "𠮷" == "\uD842\uDFB7"// new String ops"𠮷".codePointAt(0) == 0x20BB7// for-of iterates code pointsfor(var c of "𠮷") { console.log(c);}
// lib/math.jsexport function sum(x, y) { return x + y;}export var pi = 3.141593;
// app.jsimport as math from "lib/math";console.log("2π = " + math.sum(math.pi, math.pi));
Some additional features include
export default
and export
:
// lib/mathplusplus.jsexport from "lib/math";export var e = 2.71828182846;export default function(x) { return Math.exp(x);}
### Module Loaders
Module loaders support:- Dynamic loading- State isolation- Global namespace isolation- Compilation hooks- Nested virtualizationThe default module loader can be configured, and new loaders can be constructedto evaluate and load code in isolated or constrained contexts.Not part of ES2015
This is left as implementation-defined within the ECMAScript 2015 specification. The eventual standard will be in WHATWG's , but that is currently a work in progress. What is below is from a previous ES2015 draft.
// Dynamic loading – ‘System’ is default loaderSystem.import("lib/math").then(function(m) { alert("2π = " + m.sum(m.pi, m.pi));});// Create execution sandboxes – new Loadersvar loader = new Loader({ global: fixup(window) // replace ‘console.log’});loader.eval("console.log(\"hello world!\");");// Directly manipulate module cacheSystem.get("jquery");System.set("jquery", Module({$: $})); // WARNING: not yet finalized
Additional polyfill needed
Since Babel defaults to using common.js modules, it does not include the polyfill for the module loader API. Get it here.
### Map + Set + WeakMap + WeakSetEfficient data structures for common algorithms. WeakMaps provides leak-freeobject-key’d side tables.Using Module Loader
In order to use this, you'll need to tell Babel to use the
system
module formatter. Also be sure to check out
### ProxiesProxies enable creation of objects with the full range of behaviors available tohost objects. Can be used for interception, object virtualization,logging/profiling, etc.Support via polyfill
In order to support Maps, Sets, WeakMaps, and WeakSets in all environments you must include the Babel polyfill.
// Proxying a normal objectvar target = {};var handler = { get: function (receiver, name) { return
Hello, ${name}!
; }};var p = new Proxy(target, handler);p.world === "Hello, world!";
There are traps available for all of the runtime-level meta-operations:
// Proxying a function objectvar target = function () { return "I am the target"; };var handler = { apply: function (receiver, …args) { return "I am the proxy"; }};var p = new Proxy(target, handler);p() === "I am the proxy";
var handler ={ // target.prop get: …, // target.prop = value set: …, // 'prop' in target has: …, // delete target.prop deleteProperty: …, // target(…args) apply: …, // new target(…args) construct: …, // Object.getOwnPropertyDescriptor(target, 'prop') getOwnPropertyDescriptor: …, // Object.defineProperty(target, 'prop', descriptor) defineProperty: …, // Object.getPrototypeOf(target), Reflect.getPrototypeOf(target), // target.proto, object.isPrototypeOf(target), object instanceof target getPrototypeOf: …, // Object.setPrototypeOf(target), Reflect.setPrototypeOf(target) setPrototypeOf: …, // for (let i in target) {} enumerate: …, // Object.keys(target) ownKeys: …, // Object.preventExtensions(target) preventExtensions: …, // Object.isExtensible(target) isExtensible :…}
### SymbolsSymbols enable access control for object state. Symbols allow properties to bekeyed by eitherUnsupported feature
Due to the limitations of ES5, Proxies cannot be transpiled or polyfilled. See support in .
string
(as in ES5) or symbol
. Symbols are a new primitivetype. Optional name
parameter used in debugging - but is not part of identity.Symbols are unique (like gensym), but not private since they are exposed viareflection features like Object.getOwnPropertySymbols
.### Subclassable Built-insIn ES2015, built-ins like
(function() { // module scoped symbol var key = Symbol("key"); function MyClass(privateData) { this[key] = privateData; } MyClass.prototype = { doStuff: function() { … this[key] … } }; // Limited support from Babel, full support requires native implementation. typeof key === "symbol"})();var c = new MyClass("hello")c["key"] === undefined
Array
, Date
and DOM Element
s can be subclassed.
// User code of Array subclassclass MyArray extends Array { constructor(…args) { super(…args); }}var arr = new MyArray();arr[1] = 12;arr.length == 2
### Math + Number + String + Object APIsMany new library additions, including core Math libraries, Array conversionhelpers, and Object.assign for copying.Partial support
Built-in subclassability should be evaluated on a case-by-case basis as classes such as
HTMLElement
can be subclassed while many such asDate
,Array
andError
cannot be due to ES5 engine limitations.
Number.EPSILONNumber.isInteger(Infinity) // falseNumber.isNaN("NaN") // falseMath.acosh(3) // 1.762747174039086Math.hypot(3, 4) // 5Math.imul(Math.pow(2, 32) - 1, Math.pow(2, 32) - 2) // 2"abcde".includes("cd") // true"abc".repeat(3) // "abcabcabc"Array.from(document.querySelectorAll("
")) // Returns a real ArrayArray.of(1, 2, 3) // Similar to new Array(…), but without special one-arg behavior[0, 0, 0].fill(7, 1) // [0,7,7][1,2,3].findIndex(x => x == 2) // 1["a", "b", "c"].entries() // iterator [0, "a"], [1,"b"], [2,"c"]["a", "b", "c"].keys() // iterator 0, 1, 2["a", "b", "c"].values() // iterator "a", "b", "c"Object.assign(Point, { origin: new Point(0,0) })
### Binary and Octal LiteralsTwo new numeric literal forms are added for binary (Limited support from polyfill
Most of these APIs are supported by the Babel polyfill. However, certain features are omitted for various reasons (e.g.
String.prototype.normalize
needs a lot of additional code to support). You can find more polyfills .
b
) and octal (o
).
0b111110111 === 503 // true0o767 === 503 // true
### PromisesPromises are a library for asynchronous programming. Promises are a first classrepresentation of a value that may be made available in the future. Promises areused in many existing JavaScript libraries.Only supports literal form
Babel is only able to transform
0o767
and notNumber("0o767")
.
### Reflect APIFull reflection API exposing the runtime-level meta-operations on objects. Thisis effectively the inverse of the Proxy API, and allows making callscorresponding to the same meta-operations as the proxy traps. Especially usefulfor implementing proxies.Support via polyfill
In order to support Promises you must include the Babel polyfill.
var O = {a: 1};Object.defineProperty(O, 'b', {value: 2});O[Symbol('c')] = 3;Reflect.ownKeys(O); // ['a', 'b', Symbol(c)]function C(a, b){ this.c = a + b;}var instance = Reflect.construct(C, [20, 22]);instance.c; // 42
### Tail CallsCalls in tail-position are guaranteed to not grow the stack unboundedly. Makesrecursive algorithms safe in the face of unbounded inputs.Support via polyfill
In order to use the Reflect API you must include the Babel .