On Github khan4019 / ES6
June 1997
June 1998
December 1999
Abandoned
December 2009
December 2014
already started ES7?
function foo(a, b) { a = typeof a !== 'undefined' ? a : 42; b = typeof b !== 'undefined' ? b : 'default_b'; console.log('a = '+a); console.log('b = '+b); }
foo(); //a = 42, b = 'deafult_b' foo(3); //a = 3, b = 'deafult_b' foo(3,'newBee'); //a = 3, b = 'newBee'
function foo(a, b) { a = a || 42; b = b || 'default_b'; console.log('a = '+a); console.log('b = '+b); }
foo(); //a = 42, b = 'deafult_b' foo(3); //a = 3, b = 'deafult_b' foo(3,'newBee'); //a = 3, b = 'newBee'
function foo(a=42, b='default_b') { console.log('a = '+a); console.log('b = '+b); }
foo(); //a = 42, b = 'deafult_b' foo(3); //a = 3, b = 'deafult_b' foo(3,'newBee'); //a = 3, b = 'newBee'ES6: default value Firefox: default Parameters
var myShortObj = {x, y}; console.log(myShortObj); // myShortObj ={x:x, y:y};
function justLooping() { for (var i = 0; i < 5; i++) { console.log('for nothing'); } } justLooping();
function justLooping() { for (var i = 0; i < 5; i++) { console.log('for nothing'); } console.log(i); //i=5 } justLooping();
function justLooping() { console.log(i); //i=undefined for (var i = 0; i < 5; i++) { console.log('for nothing'); } console.log(i); //i=5 } justLooping();
function justLooping() { //i not here. Reference Error for (let i = 0; i < 5; i++) { console.log('for nothing'); } //i not here. Reference Error } justLooping();
function justLooping() { //i not here. Reference Error for (let i = 0; i < 5; i++) { var mySquare = i*i; } //i not here. Reference Error console.log(mySquare); // ReferenceError: } justLooping();
function foo(a, b, c, ...others){ return others; } foo(1,2,3,4,5,6);//[4,5,6]; foo(1,2,3);//[]; foo();//[];MDN: arguments
let a = [0, 1, 2]; let [x, ...xs] = a; // x = 0 // xs = [1, 2] [y, ...ys = [0]; // y = 0, ys = [] [z, ...zs] = []; // z = undefined, zs = []Spec: Spread
var square = function(a) { return a * b; };
var square = a => a*a;
var add = function(a, b) { return a + b; }; //with ES6 var add = (a, b) => a+b;Harmony: Arrow Function
var sum = function() { return 1 + 2; }; //ES 6 var sum = () => 1 + 2;
//change this for beter understanding let fives = []; nats.forEach(v => { if (v % 5 === 0) fives.push(v); });
var myObj = id => ({Id: id});
var getTempUser = function(id) { return { id: id, name: "Temp" }; }; //ES 6 var getTempUser = id => ({ id: id, name: "Temp" });Nicholas Zakas: ES6 Arrow Function
function MyClickHandler { var that = this; //var self = this; var elem = document.getElementById('myDiv'); elem.addEventListener('click', function () { console.log('CLICK'); that.handleClick(); }); } MyClickHandler.prototype.handleClick = function () { ... };
//from Harmony: make it better // ''=>'' has only lexical ''this'', no dynamic ''this'' const obj = { method: function () { return () => this; } };
function Friend(name, city){ this.name = name; this.city = city; } Friend.prototype.toString = function(){ return '('+this.name+':'+this.city+')'; }spec: Class Classes in ES6
class Friend{ constructor(name, city){ this.name = name; this.city = city; } toString(){ return '('+this.name+':'+this.city+')'; } }Harmony: Class
function DevFriend(name, city, favLang){ Friend.call(this, name, city); this.favLang = favLang; } DevFriend.prototype = Object.create(Friend.prototype); DevFriend.prototype.constructor = DevFriend; DevFriend.prototype.toString = function(){ return this.favLang + ' '+ Friend.prototype.toString.call(this); }Axel R.: Fluent slides
class DevFriend extends Friend { constructor(name, city, favLang) { //same super.constructor(name, city) super(name, city); this.favLang = favLang; } toString() { return this.favLang+' '+super(); } }
class Friend { static stupidFriend() { return new Friend('Stupid', 'Chicago'); } constructor(name, city) { this.name = name; this.city = city; } } var bss = Friend.stupidFriend();
function foo(arg0, { opt1, opt2 }) { return [opt1, opt2]; } foo(0, { opt1: 'a', opt2: 'b' }); //['a', 'b']
var obj = {first:'Java', last:'Script'}; let { first:f, last:l } = obj; console.log( f +' '+ l); //Java Script
let [ x, y ] = ['a', 'b']; let [ x, y, ...rest ] = ['a', 'b', 'c','d']; //x='a', y='b', rest=['c','d'] [ x, y] = [y, x]; //swap
//Refutable (default) var obj = {a:x, b:y} = {a:3}; //fails
//Irrefutable (default) var obj = {a:x, ?b:y} = {a:3}; //x=3, y = undefined
//refutable (default) var obj = {a:x, b:y=5} = {a:3}; //x=3, y=5
module "foo" { export let x = 42; }
import { y } from "foo";
//If a module defines a default export //then import default export by omitting the curly braces module "foo" { export default function() { console.log("hello!") } } import foo from "foo"; foo(); // hello!harmony: Module
let sym = Symbol(); console.log(typeof sym); // symbol
let red = Symbol(); let green = Symbol(); let blue = Symbol(); function handleColor(color) { switch(color) { case red: ... case green: ... case blue: ... } }Copied from slides of Axel Rauschmayer
let specialMethod = Symbol(); let obj = { // computed property key [specialMethod]: function (arg) { ... } }; obj[specialMethod](123); Shorter – method definition syntax: let obj = { [specialMethod](arg) { ... } };Copied from slides of Axel Rauschmayer
templateHandler 'Hello ${first} ${last}!' templateHandler(['Hello ', ' ', '!'], first, last)
let map = new Map(); let obj = {}; map.set(obj, 123); console.log(map.get(obj)); // 123 console.log(map.has(obj)); // true map.delete(obj); console.log(map.has(obj)); // falseMDN: Map Map and Set
A collection of values without duplicates.
let set1 = new Set(); set1.add('hello'); console.log(set1.has('hello')); // true console.log(set1.has('world')); // false let set2 = new Set([3,2,1,3,2,3]); console.log(set2.values()); // 1,2,3MDN: set
var items = new Set(); items.add(5); items.add("5"); items.add(5); // oops, duplicate - this is ignored console.log(items.size()); // 2Nicholas: Sets
function createArrayIterator(arr) { let index = 0; return { next() { if (index < arr.length) { return { done: false, value: arr[index++] }; } else { return { done: true }; } } } }MDN: generator and Iterator
function* generatorFunction() { yield 1; yield 2; } let genObj = generatorFunction(); genObj.next() //{ done: false, value: 1 } genObj.next() //{ done: false, value: 2 } genObj.next() //{ done: true }2ality: Iterators-generators
var planets = ["Hello", "Java", "Script", "Devs"]; for (p in planets) { console.log(p); // 0,1,2,3 } var es6 = { edition: 6, committee: "TC39", standard: "ECMA-262" }; for (e in es6) { console.log(e); // edition, committee, standard }
var planets = ["Mercury", "Venus", "Earth", "Mars"]; for (p of planets) { console.log(p); // Mercury, Venus, Earth, Mars }
for (word of ["one", "two", "three"]) { alert(word); } var s = Set([1, 3, 4, 2, 3, 2, 17, 17, 1, 17]); for (var v of s) { alert(v); } // Iterating over a Map produces key-value pairs: arrays of length 2. var m = new Map; m.set("one", 1); m.set("two", 2); for (var [name, value] of m) { alert(name + " = " + value); }Harmony:Iterator