ECMAScript 6

C4ce16f549c450f4759eb37f5d5d1a63?s=47 othree
July 18, 2014

ECMAScript 6

talk at COSCUP 2014 (http://coscup.org/2014/)

C4ce16f549c450f4759eb37f5d5d1a63?s=128

othree

July 18, 2014
Tweet

Transcript

  1. 3.

    Self Intro @othree https://blog.othree.net twitter web standards murmur flickr photo

    guy for confs github html5.vim, tern-coffee… ntust phd candidate
  2. 5.

    1996 1997 1998 1999 History JavaScript 1.0 JScript 1.0 JavaScript

    1.1 JScript 2.0 JScript 3.0 JavaScript 1.2 JavaScript 1.3 Netscape Microsoft
  3. 8.

    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
  4. 9.

    History ⚈ Browser War ⚈ ES3 most supported ⚈ ES4

    abandoned ⚈ ES5 current ⚈ ES6 talking today 1999 2009 2014
  5. 13.

    What is ES Now ⚈ ECMAScript is spec ⚈ JavaScript

    is implementation by Mozilla ⚈ IE’s: JScript ⚈ Host by ECMA International
  6. 14.

    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
  7. 16.

    ES6 ⚈ Next world wide web scripting language ⚈ Lots

    of new feature ⚈ Also called ECMAScript Harmony
  8. 18.
  9. 27.
  10. 28.

    Template Literals var name = 'world';! ! var greeting =

    `hello ${name}`;! ! greeting; //hello world;
  11. 29.
  12. 31.

    Arrow Function var square = (x) => {! return x

    * x;! };! ! var square2 = x => x * x; http://mdn.io/arrow
  13. 32.

    Arrow Function // Empty function body! var foo = (x)

    => {}! ! // Single parameter! var foo = x => {}! ! // No parameter! var foo = () => {}! ! // More parameters! var foo = (x, y) => {}
  14. 33.

    Arrow Function // Single expression! var foo = (x) =>

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

    Rest Parameters function foo(x = 5, ...rest) {! rest;! }!

    ! foo(1, 2, 3, 4, 5, 6);! // [2,3,4,5,6]
  16. 37.

    Spread Operator function f(x, y, z) { }! var args

    = [0, 1, 2];! ! f.apply(null, args);! ! f(...args);
  17. 38.

    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]
  18. 40.

    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
  19. 42.

    Class class Counter {! constructor() {! this.count = 0;! }!

    tick() {! this.count++;! }! get count() {! return this.count;! }! }
  20. 43.

    Class Extends class 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
  21. 45.

    Map ⚈ Like object, {…} ⚈ Key, value pair data

    structure ⚈ Use non-string data as key ⚈ Native object’s key will use toString
  22. 46.

    Map m = 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
  23. 48.

    Set ⚈ Like array, […] ⚈ Can’t get value at

    specific index ⚈ Use for…of
  24. 49.

    Set s = new Set();! s.add('A');! s.add('B');! s.add('C');! ! for

    (v of s) {! console.log(v);! }! // A, B ,C
  25. 51.
  26. 56.

    Iterator ⚈ A new interface in ES spec ⚈ User

    can implement custom iterator ⚈ An object with next method http://mdn.io/iterator
  27. 57.

    iterator.next ⚈ Return an object with value and done! ⚈

    value is next item’s value ⚈ done shows is this iterator finished ⚈ Can’t reuse
  28. 59.

    Generator ⚈ Like idMaker ⚈ Generator is a function, generate

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

    Generator function idMaker() {! var index = 0;! return {!

    next: function () {! return {! value: index++,! done: false! };! }! };! }
  30. 63.

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

    yield index++;! } http://mdn.io/generator
  31. 65.

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

    yield index++;! } http://mdn.io/generator This is a generator
  32. 66.

    First Call function* idMaker(){! var index = 0;! while(index <

    6)! yield index++;! } http://mdn.io/generator return starts here
  33. 67.

    Second Call function* idMaker(){! var index = 0;! while(index <

    6)! yield index++;! } http://mdn.io/generator return starts here
  34. 70.

    Iterable ID = {};! ! 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
  35. 72.
  36. 73.

    Comprehension var ns = [1, 2, 3, 4];! ! var

    dbls = [for (i of ns) i*2];! ! dbls; // [2, 4, 6, 8] ᾏ≌ൔ
  37. 75.

    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]
  38. 76.

    Conditional Comprehension var ns = [1, 2, 3, 4];! !

    [for (i of ns) if (i % 2) i];! //[1, 3]
  39. 77.

    Comprehension for Iterator var ns = [1, 2, 3, 4];!

    ! (for (i of ns) if (i % 2) i);! //iterator with values [1, 3]
  40. 80.
  41. 82.
  42. 83.

    Web

  43. 84.

    ES6 for Web ⚈ Precompile ES6 to ES5 ⚈ traceur-compiler

    ⚈ from Google ⚈ AngularJS 2.0 https://github.com/google/traceur-compiler
  44. 85.
  45. 86.

    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
  46. 87.

    es6-skeleton ⚈ A project seed ⚈ Based on gulp ⚈

    browserify, es6ify ⚈ livereload
  47. 89.

    ES.future ES7 ES8 guards macros contracts parallel arrays (SIMD) event

    loop concurrency http://www.2ality.com/2011/09/es6-8.html
  48. 94.
  49. 95.
  50. 96.
  51. 98.

    let x :: Number = 37;! ! function f(p ::

    Int) :: cType {}! ! let o = {! a :: Number : 42,! b: “b"! };
  52. 99.

    let x :: Number = 37;! ! function f(p ::

    Int) :: cType {}! ! let o = {! a :: Number : 42,! b: “b"! };
  53. 100.

    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/
  54. 108.

    Conclusion ⚈ ES6 is coming ⚈ You can use it

    today ⚈ Get ready for ES7, 8, 9, 10, 11
  55. 109.

    Q&A