Talk about ECMAScript 6 at YAPC::Asia Tokyo 2015
http://yapcasia.org/2015/talk/show/44721562-10e4-11e5-88a0-d7f07d574c3a
https://www.youtube.com/watch?v=oSPv5IPDSxE
9. âBrendan Eich
from Effective JavaScript
âMy solution to the challenging
requirements and crazy-short schedule
was to make JavaScript extremely
malleable from the start.â
https://www.ďŹickr.com/photos/jsconf/4587502948/
11. JavaScript has many pitfallsâŚ
⢠Prototype inheritance (No class)
⢠new, this
⢠Function scope (No block scope)
⢠Global variables (No module system)
⢠Hoisting
⢠NaN, undeďŹned
⢠typeof null
⢠with, eval
12. Example: No Class
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
console.log("Hello, I'm " + this.name);
};
var bob = new Person("Bob");
bob.greet();
// "Hello, I'm Bob"
⢠Use Prototype to emulate Classes
13. If you miss "new", dangerous!
function Person(name) {
this.name = name;
}
// Oh! You forget `new`
var bob = Person("Bob");
console.log(bob); // undefined
// Global leak!!!!!!!!!!
console.log(window.name); // "Bob"
19. Practice: Be sure to call with "new"
function Person(name) {
// check!
if (this instanceof Person) {
return new Person(name);
}
this.name = name;
}
// without `new`
var bob = Person("Bob");
bob.greet();
// "Hello, I'm Bob"
24. ES6 Classes: Simple!
class Person {
constructor(name) {
this.name = name;
}
greet() {
console.log("Hello, I'm " + this.name);
}
}
var bob = new Person("Bob");
bob.greet();
// without `new`
var bob = Person("Bob"); // Error!
25. Classes based on Prototype
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
console.log("Hello, I'm " + this.name);
};
var bob = new Person("Bob");
bob.greet();
// "Hello, I'm Bob"
OUT OF DATE!
26. Be sure to call with `new`
function Person(name) {
// check!
if (this instanceof Person) {
return new Person(name);
}
this.name = name;
}
// without `new`
var bob = Person("Bob");
bob.greet();
// "Hello, I'm Bob"
OUT OF DATE!
27. New ES6 features deprecates
yesterdayâs best-practices
No more altJS!
https://www.ďŹickr.com/photos/jorge-11/2765216505/
30. You can use ES6 now!
⢠Modern browsers and io.js (node.js) support â¨
half of ES6 features.
⢠Safari 9 (WebKit) will support many ES6 features soon.
⢠Except for IE11âŚ
31. Transpiler and polyďŹll
⢠ES6 Transpiler:â¨
source code converter from ES6 to ES5/ES3
⢠ES6 PolyďŹll:â¨
library to provide ES6 built-in classes,
functions and objects for ES5/ES3
33. Babel
⢠The most compatible (71%) ES6 transpiler
⢠Integrated with core-js (polyďŹll library)
⢠Usage:
⢠CLI: npm i -g babel
⢠Browserify: babelify
⢠REPL on the Web (Try it out!)
40. Prefer arrow function
// ES5 old function
var add = function(a, b) {
return a + b;
};
// ES6 arrow function!
var add = (a, b) => {
return a + b;
};
var add = (a, b) => a + b;
var square = n => n * n;
// good for array filter chains
[1, 2, 3, 4].filter(n => n % 2 === 0).map(n => n * n);
41. Assign âthisâ to âselfâ
var john = {
name: "John",
helloLater: function() {
// save `this` as `self`
var self = this;
setTimeout(function() {
// `this` is not available. use `self`.
console.log("Hello, I'm " + self.name);
}, 1000);
}
}
john.helloLater();
// "Hello, I'm John" after 1sec
OUT OF DATE!
42. Arrow function donât need "self"
let john = {
name: "John",
helloLater: function() {
// use arrow function
setTimeout(() => {
// `this` is available here!
console.log("Hello, I'm " + this.name);
}, 1000);
}
}
john.helloLater();
// "Hello, I'm John" after 1sec
44. ES6 Classes
class Person {
constructor(name) {
this.name = name;
}
greet() {
console.log("Hello, I'm " + this.name);
}
}
var bob = new Person("Bob");
bob.greet();
45. Classes based on Prototype
function Person(name) {
this.name = name;
}
Person.prototype.greet = function() {
console.log("Hello, I'm " + this.name);
};
var bob = new Person("Bob");
bob.greet();
// "Hello, I'm Bob"
OUT OF DATE!
46. Handmade inheritance function
// handmade function to extend
function __extends(child, parent) {
for (var key in parent) {
if (Object.prototype.hasOwnProperty.call(parent, key)) {
child[key] = parent[key];
}
}
function ctor() {
this.constructor = child;
}
ctor.prototype = parent.prototype;
child.prototype = new ctor();
child.__super__ = parent.prototype;
return child;
};
function Programmer(name, language) {
Person.call(this, name);
this.language = language;
}
__extends(Programmer, Person);
OUT OF DATE!
47. ES6 Class inheritance
class Programmer extends Person {
constructor(name, language) {
super(name);
this.language = language;
}
greet() {
super.greet();
console.log("I like " + this.language);
}
}
var bob = new Programmer("Bob", "JavaScript");
bob.greet();
// "Hello, I'm Bob"
// "I like JavaScript"
⢠with "extends" and "super"
⢠can extends built-in classes like "Error"
49. Module pattern
// Global module
var myModule = (function () {
// Module object
var module = {},
privateVariable = "Hello World";
function privateMethod() {
// ...
}
module.publicProperty = "Foobar";
module.publicMethod = function () {
console.log( privateVariable );
};
return module;
})();
OUT OF DATE!
50. CommonJS Modules
// import
var foo = require('foo');
foo();
// export
exports.bar = function() {
console.log('bar');
}
OUT OF DATE!
⢠node.js/npm friendly
⢠browserify/webpack to build for browser
51. ES6 Modules
⢠run anywhere (browsers, node.jsâŚ) if ES6 available
⢠easy to parse statically
⢠strict mode by default in modules
// export (./module.js)
export var foo = "foo!";
export function bar() {}
export class Baz {
baz() {}
}
// import
import {foo, bar, Baz} from "./module";
console.log(foo); // "foo!"
bar();
new Baz();
52. Write âuse strict;â
⢠Strict mode is useful even in ES6.
⢠In ES6 Module, always strict mode!
⢠You donât have write âuse strict;â in ES6 modules.
"use strict";
// Error!
with (obj) {}
// Error!
var obj = {a: 1, a: 1};
OUT OF DATE!
54. ES5 has only function scope
function foo() {
var num = 1;
// ... too many statements
if (true_condition) {
// same scope! overwrite above `num`!
var num = 2;
// .. some process
}
console.log(num);
// 2 !!!
}
57. Place âvarâs at the top of the scope
⢠for function scope and hoisting
function init() {
// `var` once at the top of the scope!
var i,
cell,
cells = document.getElementsByTagName('td');
for (i = 0; i < cells.length; i++) {
cell = cells[i];
cell.addEventListener('click', function() {
cell.style.backgroundColor = '#00F';
}, false);
}
}
OUT OF DATE!
58. Use ES6 let or const anytime!
⢠let and const create block scope
⢠no hoisting
⢠no more "var"!
function foo() {
let num = 1;
// ... too many statements
if (true_condition) {
// different scope!
let num = 2;
}
console.log(num);
// 1
}
59. with for-loop
⢠each iterate creates its own block scope
for (let i = 0; i < 5; i++) {
// new block scope is created for each iteration
setTimeout(() => console.log(i), i * 100);
}
// display "1", "2", "3", "4", "5"
60. const
⢠immutable value (not immutable object)
⢠use like Java's ďŹnal
const foo = 1;
foo = 100; // Error!
const foo = 1000; // Error!
// properties are mutable
const obj = {};
obj.foo = 1; // No error
62. Incorrect default params
function add(a, b) {
// if "a" is 0, 1 is assigned to "a".
a = a || 1;
b = b || 2;
return a + b;
}
add(0, 0); // 1 + 2 = 3
⢠Incorrect, but occur frequently
63. function add(a, b) {
// correct, but awful..
if (a === undefined) {
a = 1;
}
if (b === undefined) {
b = 2;
}
return a + b;
}
add(0, 0); // 0 + 0 = 0
Handmade default params
comparing to undeďŹned
OUT OF DATE!
64. ES6 Default Parameters
// default value for each param
function add(a = 1, b = 2) {
return a + b;
}
add(); // 1 + 2 = 3
add(0); // 0 + 2 = 2
add(undefined, 0); // 1 + 0 = 1
add(0, 0); // 0 + 0 = 0
66. Use "arguments" for variable-length
arguments
function foo(first, second) {
console.log("first:", first);
console.log("second:", second);
// arguments is an ArrayLike, not an Array.
var rest = Array.prototype.slice.call(arguments, 2);
console.log("rest:", rest);
}
foo(1, 2, 3, 4, 5);
// first: 1
// second: 2
// rest: [3, 4, 5]
OUT OF DATE!
67. ES6 Rest Params instead of arguments
⢠You donât have to use `arguments`
function foo(first, second, ...rest) {
console.log("first:", first);
console.log("second:", second);
console.log("rest:", rest);
}
foo(1, 2, 3, 4, 5);
// first: 1
// second: 2
// rest: [3, 4, 5]
74. Concat with "+" or String#join()
// concat with variables
var name = 'Bob';
var str = "Hello, I'm " + name + ".";
// create multiple lines
var multi = ["line1", "line2", "line3"].join("n");
OUT OF DATE!
75. Template Literal
// interpolation
var name = 'Bob';
var str = `Hello, I'm ${name}.`;
// multiple lines
var multi =
`line1
line2
line3`;
⢠back-quoted string
88. Use Object as a dictionary
// some keys are dangerous
var obj = {};
var key = "toString";
obj[key] = "value1";
String(obj);
// TypeError: can't convert obj to string
OUT OF DATE!
⢠some special keys are dangerous
89. Use Object as a dictionary
// cannot use object as a key
var key1 = {name: "key1"};
var key2 = {name: "key2"};
obj[key1] = "value1";
obj[key2] = "value2";
console.log(obj[key1]);
// "value2"
console.log(Object.keys(obj));
// ["[object Object]"]
OUT OF DATE!
⢠cannot use an object as a key
90. ES6 Map
// no dangerous keys
let map = new Map();
map.set("toString", "value1");
map.get("toString"); // "value1"
String(map); // "[object Map]"
// object as a key
let key1 = {};
let key2 = {};
let m = new Map();
m.set(key1, "v1");
m.set(key2, "v2");
m.get(key1); // "v1"
91. ES6 Set
let set = new Set();
set.add("value1");
console.log(set.size); // 1
// unique
set.add("value1");
console.log(set.size); // 1
⢠not easy to implement Set in ES5
105. The TC39 Process: Annual
⢠TC39 committee approves acceptance for each stage.
Stage 0: Strawman (idea)
Stage 1: Proposal (problem, solution and demo/polyďŹll)
Stage 2: Draft (initial spec)
Stage 3: Candidate (review and feedback)
Stage 4: Finished (two implementations at least)
⢠Stage 4 features are published as ES201Xâ¨
on July every year.
108. Exponentiation Operator
// x ** y
let squared = 2 ** 2;
// same as: 2 * 2
let cubed = 2 ** 3;
// same as: 2 * 2 * 2
// x **= y
let a = 2;
a **= 2;
// same as: a = a * a;
113. SIMD
let a = SIMD.Float32x4(1.0, 2.0, 3.0, 4.0);
let b = SIMD.Float32x4(5.0, 10.0, 15.0, 20.0);
let c = SIMD.Float32x4.add(a,b);
// c: (6.0, 12.0, 18.0, 24.0)
⢠Single Instruction Multiple Data
⢠Vector data calculation
115. May the Babel be with you!
⢠It's too far for all browsers to support ES6.
⢠IE11 will live until Jun 10, 2023âŚ
⢠You will be able to stop transpiling features
that browsers support natively.
⢠Also ES201X features are available via Babel.
116. Design your policy:
Which ES6 features do you use?
⢠Which browsers/node.js do you support?
⢠If you need IE8 (ES3), it's not easy to use ES6âŚ
⢠Which feature is effective for your project?
⢠Is the feature easy to transpile/polyďŹll?
117. Easy to transpile/polyďŹll?
⢠No problem
Arrow function, let/const, Extended Object literal, Classesâ¨
Extended function params, Template literal, Map/Set, PromiseâŚ
⢠Be careful/Partial
Module, Generator, Symbol
⢠Hard/Impossible
WeakMap/Set, Proxy, ReďŹect, Tail Call Optimization