What is ES6/ES2015, and what does it mean for Javascript development? What does the new syntax look like, and how can we use it today? Featuring side-by-side comparisons!
latest version of the ECMAScript standard. ES6 is a significant update to the language, and the first update to the language since ES5 was standardized in 2009. Implementation of these features in major JavaScript engines is underway now. “ ” Luke Hoban - ES6 Features
Slightly quirky and inconsistent • ES5 / JavaScript 1.8 ◦ “use strict” opt-in • ES6 / JavaScript 2015 ◦ New syntax ◦ New features Versions, browsers & support All Modern Browsers Future tech / partial support
custom code • String templates improve readability • New shorthand codes • Standard ‘class’ support to replace common boilerplates • Standard ‘module’ support to replace anti-patterns & libraries
}; var tmp = getASTNode(); var op = tmp.op; var lhs = tmp.lhs; var rhs = tmp.rhs; var nums = evens.map(function (v, i) { return v + i; }); var obj = { x, y }; var { op, lhs, rhs } = getASTNode() var nums = evens.map((v, i) => v + i)
y) { return x + y }; LibMath.pi = 3.141593; // someApp.js var math = LibMath; console.log("2π = " + math.sum(math.pi, math.pi)); // otherApp.js var sum = LibMath.sum, pi = LibMath.pi; console.log("2π = " + sum(pi, pi)); // lib/math.js export function sum (x, y) { return x + y } export var pi = 3.141593 // someApp.js import * as math from "lib/math" console.log("2π = " + math.sum(math.pi, math.pi)) // otherApp.js import { sum, pi } from "lib/math" console.log("2π = " + sum(pi, pi))
‘side effects’ and memory usage • const, let and var improve readability and reduce cognitive load • Arrow functions control this, replacing common anti-patterns • Promises make asynchronous operations easier to write without callback hell and/or the need for an external library
(inputValue, options) => functionToExecute(inputValue, 'customValue', options); // rather than the old way: function(inputValue, options) { functionToExecute(inputValue, 'customValue', options); } // no more var thiz = this; $('.button').on('click', function() { thiz.doStuff(); }); // this instead $('.button').on('click', () => { this.doStuff(); });
closures • Constants remove the need for libraries like immutable.js • Class support standardises the ‘prototype’ and ‘object’ patterns • Modules support standardises CommonJS / AMD patterns • Promises standardise async behaviour and remove the need for custom polyfill libraries
‘side effects’ and memory usage • const, let and var improve readability and reduce cognitive load • Parameter enhancements replace frequently written custom code • String templates improve readability • Arrow functions control this, replacing common anti-patterns • New shorthand code reduces repetition repetition • Proper class support replaces common boilerplates and antipatterns • Modules enable better code organisation without file concatenation steps or external module-loader libraries • Promises make asynchronous operations easier without the need for an external library