JavaScript...

IS DEAD!




#WAT


Long Live...

ECMAScript!



let, the new var



Real 

locally scoped

variables

Example



    function Foo() {
      //...
      for(var i=0; i<10; i+=1) {
        let tmp = i;
        console.log(tmp);  // 0, 1, 2...
      }
      //...
      console.log(i);  // 10
      console.log(tmp);  // undefined
    }  
    


Bits, Bytes, Blobs




C-like

Structured types

Example


    var Point2D = new StructType({ x: uint32, y: uint32 });
    var Color = new StructType({ r: uint8, g: uint8, b: uint8 });
    let Pixel = new StructType({ point: Point2D, color: Color });
    let Line = new ArrayType(Pixel, 2);
    let line = new Line({
     { 
       point: { x: 10, y: 10},
       color: { r: 255, g: 255, b: 255}
     },
     {
       point: { x: 200, y: 30},
       color: { r: 0, g: 100, b: 255 }
     }
    });
    


Destructuring



for 

Python-Ruby-PHP 

fans!

Example



    var { r, g, b } = line.color;
    let [x, y] = circle.center;
    let [a, b] = [b, a];
    


Rest, Spread

default params

Default params


before

    function king_of_the_north(who) {
        return is_the_new_king(who || 'john-snow.gif');
    }
    
after

    function king_of_the_north(who='john-snow.gif') {
        return is_the_new_king(who);
    }
    

Rest params

before

    function all_kings_of_the_north() {
        var kings = Array.prototype.slice.call(arguments);
        kings.forEach(function(king){
            king.i_am_the_king();
        });
    }
    
after

    function all_kings_of_the_north(...kings) {
        kings.forEach(function(king){
            king.i_am_the_king();
        });
    }
    
IE10+
FF24+
CH35+












Comprehension

Example


before

    [1,2,3].map(function(i){
        return i*i;
    });
    

after
        
    [ for (i of [1,2,3]) i*i ]
    


Generators


Run

To

Completion...


...Run

Stop

Run!

Example



    function* fibonacci() {
        let [prev, curr] = [0, 1];
        for (;;) {
            [prev, curr] = [curr, prev + curr];
            yield curr;
        }
    }

    let seq = fibonacci();
    console.log(seq.next().value);
    console.log(seq.next().value);
    console.log(seq.next().value);

    


Proxies



JavaScript 

Object 

Wrapping


MetaProgramming API

Example


    var handler = {
        get: function(myProxy, name){
            return true;
        }
    };
    var king_of_the_north = Proxy.create(handler);
    king_of_the_north.is_the_king; // true
    king_of_the_north.is_the_king = false; // Error
    

Other methods


set(receiver, name, val), delete(name), enumerate(), 
fix(), has(name), hasOwn(name), keys(), iterate()...


Maps, WeakMaps



WeakMap

(or Dictionnary)

Example


    var john_snow = {
        is_king: true,
        really: function() {
            return !john_snow.is_king;
        }
    };

    var kings_of_the_north = new WeakMap();
    kings_of_the_north.set(john_snow, john_snow);

    kings_of_the_north.get(john_snow);
    kings_of_the_north.get(john_snow).is_king;
    kings_of_the_north.get(john_snow).really();
    

Map vs. WeakMap



  • Maps
    • Iterable
    • Non-GC keys

  • WeakMaps
    • Non-Iterable
    • GC keys


Modules




CommonJS  AMD

Named exports


        //------ kings.js ------
        var kings = {
            john: {
                is_king: true
            }
        }
        function is_king() {
            return kings.john.is_king;
        }

        export { kings, is_king };
    

        
        //------ main.js ------
        import { is_king, kings } from 'kings';
    

Default exports


    //------ kings.js ------
    var K = {
        kings: {
            john: {
                is_king: true
            }
        },
        is_king() {
            return kings.john.is_king;
        }
    }
    export default K;



    //------ main.js ------
    import K from 'kings';








#thanks





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

https://developer.mozilla.org/en-US/docs/Web/JavaScript/New_in_JavaScript/ECMAScript_6_support_in_Mozilla

http://addyosmani.com/blog/tracking-es6-support/
Wassim Chegham
aka — Maneki Nekko

  ECMAScript Jedi