Top Banner
ES6 Cory Forsyth @bantic
50

Explaining ES6: JavaScript History and What is to Come

Jul 28, 2015

Download

Internet

Cory Forsyth
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Explaining ES6: JavaScript History and What is to Come

ES6Cory Forsyth

@bantic

Page 2: Explaining ES6: JavaScript History and What is to Come

Cory Forsyth @bantic

Page 3: Explaining ES6: JavaScript History and What is to Come

JavaScript History

Page 4: Explaining ES6: JavaScript History and What is to Come

JavaScript is not a Language

• JavaScript is an implementation of a language

• ActionScript was another implementation

• The name of the language is: ECMAScript

• JavaScript : ECMAScript :: Rubinius/JRuby : Ruby

Page 5: Explaining ES6: JavaScript History and What is to Come

JavaScript History• Invented at Netscape by Brendan Eich

• Mocha -> LiveScript -> JavaScript

• 1996 -> Standardization taken by Ecma

• Renamed -> ECMAScript (trademark reasons)

Page 6: Explaining ES6: JavaScript History and What is to Come

ECMAScript• What is Ecma?

• “Ecma International is an industry association … dedicated to the standardization of Information and Communication Technology”

• Its website slogan is: “Standards@Internet Speed”

Page 7: Explaining ES6: JavaScript History and What is to Come
Page 8: Explaining ES6: JavaScript History and What is to Come

Ecma Org Chart

Page 9: Explaining ES6: JavaScript History and What is to Come

Ecma Org Chart• TC = Technical Committee

• TC39 = Technical Committee tasked with ECMAScript ®

• TC39 Official Scope: “Standardization of the general purpose, cross platform, vendor-neutral programming language ECMAScript.”

• Ecma = European Computer Manufacturers Association

Page 10: Explaining ES6: JavaScript History and What is to Come

ECMAScript

• 1997: ECMA-262: Language Standard

• On github (now): tc39/ecma262

• 1998 ES2

• 1999 ES3

Page 11: Explaining ES6: JavaScript History and What is to Come

ECMAScript• 2000: ES4

Page 12: Explaining ES6: JavaScript History and What is to Come

ECMAScript• 2000: ES4

Page 13: Explaining ES6: JavaScript History and What is to Come

ECMAScript• 2000: ES4

• 2005: ES4

• Mozilla and Macromedia

• Big changes, major leap from ES5

Page 14: Explaining ES6: JavaScript History and What is to Come

ECMAScript• 2000: ES4

• 2005: ES4

• Mozilla and Macromedia

• Big changes, major leap from ES5

• Divisive, Yahoo & Microsoft opposed

Page 15: Explaining ES6: JavaScript History and What is to Come

ECMAScript• 2000: ES4

• 2005: ES4

• Mozilla and Macromedia

• Big changes, major leap from ES5

• Divisive, Yahoo & Microsoft opposed

Page 16: Explaining ES6: JavaScript History and What is to Come

ES5• 2009 in Oslo

• “Modern” JavaScript

• Originally v3.1 (incremental change)

• “Harmony”

Page 17: Explaining ES6: JavaScript History and What is to Come

ES6• Will be released in 2015

• After this, versions will be released yearly:

• ES2015

• ES2016

• …

Page 18: Explaining ES6: JavaScript History and What is to Come

ES2015: What’s New?

Page 19: Explaining ES6: JavaScript History and What is to Come

ES2015

• Syntax Changes

• New features

• Fixes

• Superset of ES5

Page 20: Explaining ES6: JavaScript History and What is to Come

if (true) {var x = 'yes';

}console.log(x); // 'yes'

if (true) {let x = 'yes';

}console.log(x); // ReferenceError

var is hoisted

let is block scope

`let` there be light scope

Page 21: Explaining ES6: JavaScript History and What is to Come

hoisting

function() {console.log(x); // undefinedvar x = 'foo';console.log(x); // ‘foo’

}

Page 22: Explaining ES6: JavaScript History and What is to Come

hoisting

function() { var x;

console.log(x); // undefinedx = 'foo';console.log(x); // ‘foo’

}

Page 23: Explaining ES6: JavaScript History and What is to Come

`let` : temporal dead zone

function() {console.log(x); // ReferenceError// TDZlet x = 'foo';console.log(x); // ‘foo’

}

Page 24: Explaining ES6: JavaScript History and What is to Come

`let` there be sanity

let funcs = [];for (var i=0; i < 5; i++) { funcs.push(function() { alert(i); });}funcs[0]();

Page 25: Explaining ES6: JavaScript History and What is to Come

`let` there be sanity

let funcs = [];for (let i=0; i < 5; i++) { funcs.push(function() { alert(i); });}funcs[0]();

a new i for each turn of the loop

Page 26: Explaining ES6: JavaScript History and What is to Come

`const`const x = 'foo';x = 'bar'; // error

const x = {foo: 'bar'

};x.foo = 'baz'; // ok (!)

can modify properties of a const

cannot reassign const

use Object.freeze, Object.seal to prevent changes to an object

Page 27: Explaining ES6: JavaScript History and What is to Come

Arrow functions =>var x = function() {} let x = () => {}

• Has own scope

• Has `arguments`

• No implicit return

• “lexical” scope

• No `arguments`

• implicit return*

• *sometimes

Page 28: Explaining ES6: JavaScript History and What is to Come

Arrow functions =>

this.x = 'yes';let y = () => { "use strict"; console.log(this.x);};y(); // 'yes'

this.x = 'yes';var y = function(){ "use strict"; console.log(this.x);};y(); // TypeError…

`this` is undefined `this` from outer scope

No need for var that = this, `bind` with arrow functions

Page 29: Explaining ES6: JavaScript History and What is to Come

Arrow functions =>1 arg: no parens, implicit returnlet square = x => x * x;let squares = [1,2,3].map(square);console.log(squares) // [1,4,9]

Page 30: Explaining ES6: JavaScript History and What is to Come

Arrow functions =>1 arg: no parens, implicit returnlet square = x => x * x;let squares = [1,2,3].map(square);console.log(squares) // [1,4,9]

let add = (a,b) => a + b;add(2,3); // 5

2 args: needs parens, implicit return

Page 31: Explaining ES6: JavaScript History and What is to Come

Arrow functions =>1 arg: no parens, implicit returnlet square = x => x * x;let squares = [1,2,3].map(square);console.log(squares) // [1,4,9]

let add = (a,b) => a + b;add(2,3); // 5

2 args: needs parens, implicit return

let add = (a,b) => { return a + b };add(2,3); // 5

function body: use {}, explicit return

Page 32: Explaining ES6: JavaScript History and What is to Come

Arrow functions =>`arguments` from outer scope

let x = function() {return () => {

alert(arguments[0]);};

}let y = x(5);y();

uses x’s arguments

Page 33: Explaining ES6: JavaScript History and What is to Come

template stringsstring interpolation!

let foo = 'bar';console.log(`foo = ${foo}`);// "foo = bar"

let multiline = `first lineand second line

`);

multiline strings

Page 34: Explaining ES6: JavaScript History and What is to Come

destructuring assignmentlet obj = {

foo: 'bar',boo: 'baz'

};let {foo, boo} = obj;console.log(foo); // 'bar'console.log(boo); // 'baz'

let colors = ['red', 'green'];let [color1, color2] = colors;console.log(color2); // green

for objects

for arrays

Page 35: Explaining ES6: JavaScript History and What is to Come

destructuring assignmentmixed

let obj = {foo: 'bar',colors: ['red', 'green']

}let {foo, colors: [color1, color2]} = obj;console.log(foo); // barconsole.log(color2); // green

Page 36: Explaining ES6: JavaScript History and What is to Come

shorthand object assignment

same property and variable name

let foo = 'bar';

let obj = {foo,boo() {

console.log('baz');}

}

console.log(obj.foo); // barobj.boo(); // baz

function without “: function”

Page 37: Explaining ES6: JavaScript History and What is to Come

default function paramsfunction doSomething(timeout, options) {

timeout = timeout || 2000;options = options || {repeat: true};var repeat = options.repeat;setTimeout(function(){

console.log(repeat);}, timeout);

}

doSomething(0);// logs 'true' after 2000ms

ES5

Page 38: Explaining ES6: JavaScript History and What is to Come

default function paramsfunction doSomething(timeout=2000, {repeat}={repeat:true}) {

setTimeout(function(){console.log(repeat);

}, timeout);}

doSomething(0);// logs "true" right away

ES6

Page 39: Explaining ES6: JavaScript History and What is to Come

function rest paramsfunction sum(first, ...others) {

let sum = first;for (let i of others) {

sum = sum + i;}console.log(sum);

}sum(1, 2, 3, 4); // 10

others is an array of remaining args

Page 40: Explaining ES6: JavaScript History and What is to Come

spread operator

function sum2(first, ...others) {console.log(sum + Math.max(...others));

}sum2(1, 2, 4, 3); // 5 (=== 1 + 4)

Math.max(…others) is equivalent to Math.max(2,4,3)

Page 41: Explaining ES6: JavaScript History and What is to Come

classes

class Person { constructor() { this.name = 'Cory'; } sayHi() { console.log(this.name); }}

let p = new Person();p.sayHi(); // Cory

extend and super also work as expected

Page 42: Explaining ES6: JavaScript History and What is to Come

modules

named export// file a.jsexport let x = ‘yes';

export default {foo: 'bar'

};

// file b.jsimport { x } from 'a';console.log(x); // yes

import stuff from 'a';console.log(stuff.foo); // bar

named import

default export

default import

Page 43: Explaining ES6: JavaScript History and What is to Come

Other ES2015 features

• Object.is() — fixes == and === • Unicode support • Promises for async support • Symbols — a new primitive • iterators and generators • Map, Set • Object.assign() — aka `extend`, `merge` or `mixin` • String methods — `includes`, `repeat`, `startsWith` • function.name • … many more

Page 44: Explaining ES6: JavaScript History and What is to Come

What’s the status today?

Page 45: Explaining ES6: JavaScript History and What is to Come
Page 46: Explaining ES6: JavaScript History and What is to Come

How do I use it?

Page 47: Explaining ES6: JavaScript History and What is to Come
Page 48: Explaining ES6: JavaScript History and What is to Come

Babel support

• Many ES2015 (aka ES6) features and syntax • Some ES2016 (aka ES7) features and syntax • Some things transpiled directly to ES5 • Some supported via Polyfill • Some not supported yet (Proxies, in particular)

Page 49: Explaining ES6: JavaScript History and What is to Come

The JavaScript Future

• Transpiling • Not just a transitionary thing

• Support matrices • ES is a living standard • Venn Diagram of ES2015 / ES2016 / Browser Support

• As target browsers support features your code uses, turn off transpilation of those features

Page 50: Explaining ES6: JavaScript History and What is to Come

Cory Forsyth @bantic

Thank you!

• Babel and Babel REPL • Using ES6 Today • ES6 Glossary • ES6+ Compatibility Table • ES6 Overview Slides • ECMA-262 Living Standard on github • Ecma International • ES6 Learning • Aligning Ember With Web Standards • http://bit.ly/bantic-es6 — these slides

Links