Top Banner
A ECMAScript 6 othree coscup
109

ECMAScript 6

Dec 18, 2014

Download

Documents

talk at COSCUP 2014
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: ECMAScript 6

AECMAScript 6othree coscup

Page 2: ECMAScript 6

https://github.com/voodootikigod/logo.js

A

Page 3: ECMAScript 6

Self Intro

@othree https://blog.othree.net

twitter web standards murmur

flickr photo guy for confs

github html5.vim, tern-coffee…

ntust phd candidate

Page 4: ECMAScript 6

History

Syntax

Page 5: ECMAScript 6

1996

1997

1998

1999

History

JavaScript 1.0JScript 1.0JavaScript 1.1

JScript 2.0

JScript 3.0JavaScript 1.2

JavaScript 1.3

Netscape Microsoft

Page 6: ECMAScript 6

Standardization

⚈ Standard script language running on browser

⚈ Host by ECMA

Page 7: ECMAScript 6

ECMA歐洲電腦製造商協會

Page 8: ECMAScript 6

ECMAScript

⚈ Standard of JavaScript

⚈ ECMA-262, also called ECMAScript

⚈ 1.0, 2.0 published around 1997-1998

⚈ Current Edition: 5.1

http://zh.wikipedia.org/wiki/ECMAScript

Page 9: ECMAScript 6

History

⚈ Browser War

⚈ ES3 most supported

⚈ ES4 abandoned

⚈ ES5 current

⚈ ES6 talking today

1999

2009

2014

Page 10: ECMAScript 6

ES3

⚈ Most supported

⚈ IE6, 7, 8

Page 11: ECMAScript 6

ES4

⚈ Flash, ActionScript

⚈ Abandoned

Page 12: ECMAScript 6

ES5

⚈ From 3.1

⚈ Strict mode

⚈ More solid spec

Page 13: ECMAScript 6

What is ES Now

⚈ ECMAScript is spec

⚈ JavaScript is implementation by Mozilla

⚈ IE’s: JScript

⚈ Host by ECMA International

Page 14: ECMAScript 6

JavaScript

1.5 ECMAScript 3

1.6 array extras + array and string generics + E4X

1.7 Pythonic generators + iterators + let

1.8 generator expressions + expression closures

1.81 native JSON support

1.85 ECMAScript 5 compliance

1.86 more ES6 futures

http://en.wikipedia.org/wiki/JavaScript#Version_history

Page 15: ECMAScript 6

New in JavaScripthttps://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript

Page 16: ECMAScript 6

ES6

⚈ Next world wide web scripting language

⚈ Lots of new feature

⚈ Also called ECMAScript Harmony

Page 17: ECMAScript 6

Q&A

⚈ May I use today

Page 18: ECMAScript 6

Q&A

⚈ May I use today

⚈ Yes

⚈ Google is using (AngularJS 2.0)

Page 19: ECMAScript 6

ES5 You May Not Know

Page 20: ECMAScript 6

http://youtu.be/UTEqr0IlFKY

Page 21: ECMAScript 6

ES6 Features

Page 22: ECMAScript 6

Major Features

Module Class

Iterator Syntax

Page 23: ECMAScript 6

Syntax

History

Iterator���

Page 24: ECMAScript 6

let

⚈ Block scope local variable

http://mdn.io/let

Page 25: ECMAScript 6

let

{! let foo = 100;! foo; // 100!}!!foo; //undefined

Page 26: ECMAScript 6

let

for (let i = 0; i < len; i++) {! // blah!}

Page 27: ECMAScript 6

const

const foo = 100;!foo; // 100!!foo = 101;!!foo; // 100

Page 28: ECMAScript 6

Template Literals

var name = 'world';!!var greeting = `hello ${name}`;!!greeting; //hello world;

Page 29: ECMAScript 6
Page 30: ECMAScript 6

`

⚈ Grave accent

⚈ Back tick

⚈ Shell: execute command in between

Page 31: ECMAScript 6

Arrow Function

var square = (x) => {! return x * x;!};!!var square2 = x => x * x;

http://mdn.io/arrow

Page 32: ECMAScript 6

Arrow Function// Empty function body!var foo = (x) => {}!!// Single parameter!var foo = x => {}!!// No parameter!var foo = () => {}!!// More parameters!var foo = (x, y) => {}

Page 33: ECMAScript 6

Arrow Function

// Single expression!var foo = (x) => x*x!!// Multiple expression!var foo = (x) => {! let y = x * x;! // need return! return y * x;!}

Page 34: ECMAScript 6

Arrow Function

⚈ Auto return result of single expression

⚈ Lexical this , like CoffeeScript

Page 35: ECMAScript 6

Default Parameter

function foo(x = 5, y = 5) { }

Page 36: ECMAScript 6

Rest Parameters

function foo(x = 5, ...rest) {! rest;!}!!foo(1, 2, 3, 4, 5, 6);!// [2,3,4,5,6]

Page 37: ECMAScript 6

Spread Operator

function f(x, y, z) { }!var args = [0, 1, 2];!!f.apply(null, args);!!f(...args);

Page 38: ECMAScript 6

Spread Operator

var arg2 = [...args, 3, 4];!// [0,1,2,3,4]!!var arg3 = arg.push(...arg2);!// [0,1,2,0,1,2,3,4]

Page 39: ECMAScript 6

Destructing Assign

var a, b;!![a, b] = [1, 2];!//a:1, b:2

Page 40: ECMAScript 6

Destructing Assign

[a, b] = [b, a];!//swap!![a, ,[b, c]] = [1, 0, [2, 3]];!//a:1, b:2, c:3!!{lan: a, lon: b} = getPOS();!//object destructing

Page 41: ECMAScript 6

Destructing and Spread

[a, ...b] = [1, 2, 3, 4, 5];!//a:1, b:[2,3,4,5]

Page 42: ECMAScript 6

Classclass Counter {! constructor() {! this.count = 0;! }! tick() {! this.count++;! }! get count() {! return this.count;! }!}

Page 43: ECMAScript 6

Class Extendsclass People extends Counter {! constructor(people) {! this.people = people;! for (let p in people) {! this.tick();! }! }!}!!var p = new People([1,2,3,4,5]);!p.count; //5

Page 44: ECMAScript 6

Class

⚈ No multiple inheritance

⚈ Define property only in constructor

Page 45: ECMAScript 6

Map

⚈ Like object, {…}

⚈ Key, value pair data structure

⚈ Use non-string data as key

⚈ Native object’s key will use toString

Page 46: ECMAScript 6

Mapm = new Map();!m.set(true, 'T');!m.set(false, 'F');!!m.size; //2!!m.get(true); //"T"!m.get(false); //"F"!!m.get(1); // undefined

Page 47: ECMAScript 6

Map Methods

clear has

delete keys

entries set

forEach values

get

Page 48: ECMAScript 6

Set

⚈ Like array, […]

⚈ Can’t get value at specific index

⚈ Use for…of

Page 49: ECMAScript 6

Set

s = new Set();!s.add('A');!s.add('B');!s.add('C');!!for (v of s) {! console.log(v);!}!// A, B ,C

Page 50: ECMAScript 6

Set Methods

add forEach

clear has

delete values

entries

Page 51: ECMAScript 6

for…of

Page 52: ECMAScript 6

for...of

⚈ New loop method

⚈ Like CoffeeScript’s for...in

⚈ Used to loop iterable object items

Page 53: ECMAScript 6

Iterable

Array String

Map Set

Page 54: ECMAScript 6

Create Custom Iterable Object

Page 55: ECMAScript 6

Iterator���

Syntax

Use ES6 Today

Page 56: ECMAScript 6

Iterator

⚈ A new interface in ES spec

⚈ User can implement custom iterator

⚈ An object with next method

http://mdn.io/iterator

Page 57: ECMAScript 6

iterator.next

⚈ Return an object with value and done!

⚈ value is next item’s value

⚈ done shows is this iterator finished

⚈ Can’t reuse

Page 58: ECMAScript 6

Iterator

var it = idMaker();!!console.log(it.next().value);!console.log(it.next().value);!console.log(it.next().value);

Page 59: ECMAScript 6

Generator

⚈ Like idMaker

⚈ Generator is a function, generate iterator

⚈ Different invoke will create different iterator, iterate the same list.

Page 60: ECMAScript 6

Generatorfunction idMaker() {! var index = 0;! return {! next: function () {! return {! value: index++,! done: false! };! }! };!}

Page 61: ECMAScript 6

yield

⚈ yield is generator helper

⚈ Let you easy to create generator

Page 62: ECMAScript 6

yield

function* idMaker(){! var index = 0;! while(true)! yield index++;!}

http://mdn.io/generator

Page 63: ECMAScript 6

yield

function* idMaker(){! var index = 0;! while(index < 6)! yield index++;!}

http://mdn.io/generator

Page 64: ECMAScript 6

yield

⚈ * is the indicator to tell runtime

⚈ yield is return point

Page 65: ECMAScript 6

yield

function* idMaker(){! var index = 0;! while(index < 6)! yield index++;!}

http://mdn.io/generator

This is a generator

Page 66: ECMAScript 6

First Call

function* idMaker(){! var index = 0;! while(index < 6)! yield index++;!}

http://mdn.io/generator

return

starts here

Page 67: ECMAScript 6

Second Call

function* idMaker(){! var index = 0;! while(index < 6)! yield index++;!}

http://mdn.io/generator

returnstarts here

Page 68: ECMAScript 6

yield

⚈ Function end will return done: true

Page 69: ECMAScript 6

Iterable

⚈ Have generator function in the object

⚈ Under @@iterator property

Page 70: ECMAScript 6

IterableID = {};!!ID['@@iterator'] = idMaker;!//or use Symbol!ID[Symbol.iterator] = idMaker;!!for (id of ID) {! id;! //0,1,2,3,4,5!}

http://people.mozilla.org/~jorendorff/es6-draft.html#table-1

Page 71: ECMAScript 6

Iterable Features

Page 72: ECMAScript 6

for…of

Page 73: ECMAScript 6

Comprehension

var ns = [1, 2, 3, 4];!!var dbls = [for (i of ns) i*2];!!dbls; // [2, 4, 6, 8]

簡約式

Page 74: ECMAScript 6

CoffeeScript Syntax

arr = [1, 2, 3, 4];!!res = (x for x in arr);

Page 75: ECMAScript 6

2 Level Comprehension

var ms = [1, 2, 3, 4];!var ns = [2, 3, 4, 5];!![for (i of ms) for (j of ns) i*j];!// [2, 6, 12, 20]

Page 76: ECMAScript 6

Conditional Comprehension

var ns = [1, 2, 3, 4];!![for (i of ns) if (i % 2) i];!//[1, 3]

Page 77: ECMAScript 6

Comprehension for Iterator

var ns = [1, 2, 3, 4];!!(for (i of ns) if (i % 2) i);!//iterator with values [1, 3]

Page 78: ECMAScript 6

more…

⚈ Object Literal Extensions

⚈ Proxy

⚈ Modules, Import, Export

⚈ Promise

⚈ Symbol

Page 79: ECMAScript 6

Use ES6 Today

Iterator���

ECMAScript 7,8…

Page 80: ECMAScript 6
Page 81: ECMAScript 6

http://kangax.github.io/compat-table/es6/

Page 82: ECMAScript 6
Page 83: ECMAScript 6

Web

Page 84: ECMAScript 6

ES6 for Web

⚈ Precompile ES6 to ES5

⚈ traceur-compiler

⚈ from Google

⚈ AngularJS 2.0

https://github.com/google/traceur-compiler

Page 85: ECMAScript 6
Page 86: ECMAScript 6

in Develop

⚈ Need watch and compile when file changes

⚈ Use gulp to watch

⚈ gulp-traceur or es6ify to compile

⚈ https://github.com/othree/es6-skeleton

Page 87: ECMAScript 6

es6-skeleton

⚈ A project seed

⚈ Based on gulp

⚈ browserify, es6ify

⚈ livereload

Page 88: ECMAScript 6

ECMAScript 7,8…

Use ES6 Today

Conclusion

Page 89: ECMAScript 6

ES.future

ES7 ES8

guards macros

contractsparallel arrays

(SIMD)

event loop concurrency

http://www.2ality.com/2011/09/es6-8.html

Page 90: ECMAScript 6

http://youtu.be/3WgVHE5Augc

Page 91: ECMAScript 6

Type

⚈ First see in ActionScript

Page 92: ECMAScript 6

ActionScript 3.0 Cookbook

Page 93: ECMAScript 6

Type

⚈ TypeScript also has type imply syntax

Page 94: ECMAScript 6
Page 95: ECMAScript 6
Page 96: ECMAScript 6
Page 97: ECMAScript 6

Type in ES.future

⚈ Called guards

Page 98: ECMAScript 6

let x :: Number = 37;!!function f(p :: Int) :: cType {}!!let o = {! a :: Number : 42,! b: “b"!};

Page 99: ECMAScript 6

let x :: Number = 37;!!function f(p :: Int) :: cType {}!!let o = {! a :: Number : 42,! b: “b"!};

Page 100: ECMAScript 6

Benefit

⚈ Write more solid code

⚈ Better Performance

⚈ JS engine detects variable type change now

⚈ JSLint: confusion: true

http://www.html5rocks.com/en/tutorials/speed/v8/

Page 101: ECMAScript 6

Where is new Spec

Page 102: ECMAScript 6

Traceur-Compiler Doc

https://github.com/google/traceur-compiler/wiki/LanguageFeatures

Page 103: ECMAScript 6

ES Wiki

http://wiki.ecmascript.org/doku.php

Page 104: ECMAScript 6

Spec Draft

http://wiki.ecmascript.org/doku.php?id=harmony:specification_drafts

Page 105: ECMAScript 6

ES Wiki

⚈ strawman: pre proposal

⚈ harmony: TC39 approved proposal

Page 106: ECMAScript 6

TC39 Meeting Notes

https://github.com/rwaldron/tc39-notes

Page 107: ECMAScript 6

Conclusion

ECMAScript 7,8…

Page 108: ECMAScript 6

Conclusion

⚈ ES6 is coming

⚈ You can use it today

⚈ Get ready for ES7, 8, 9, 10, 11

A

Page 109: ECMAScript 6

AQ&A