Upgrade to Pro — share decks privately, control downloads, hide ads and more …

NDC London: Introduction to ES6

Jack Franklin
December 04, 2014

NDC London: Introduction to ES6

Jack Franklin

December 04, 2014
Tweet

More Decks by Jack Franklin

Other Decks in Technology

Transcript

  1. var n = [1, 2, 3, 4]; n.some(x => x

    % 2 == 0); // true n.filter(x => x % 2 != 0); //=> [1, 3] n.map(x => x * 2) //=> [2, 4, 6, 8]
  2. var n = [1, 2, 3, 4]; n.filter(x => x

    % 2 == 0) .map(x => x * x); //=> [4, 16]
  3. n.map(x => x * 2); get('url', (x, y) => {…});

    $('#foo').click(() => {…}); parens needed for 0, 2+ arguments
  4. var jack = { name: 'jack', friends: ['james', 'steve'], printFriends:

    function() { this.friends.forEach(function(f) { log(this.name, 'knows', f);
 })
 } }; jack.printFriends(); // undefined knows james // undefined knows steve
  5. var jack = { name: 'jack', friends: ['james', 'steve'], printFriends:

    function() { this.friends.forEach((f) => { log(this.name, 'knows', f);
 })
 } }; jack.printFriends(); // jack knows james // jack knows steve
  6. var someAjax = { name: 'jack', get: function() { $.getJSON(url,

    function(d) { log(this.name, d); })
 } };
  7. var nums = [1,2,3]; nums.map((x) => x * 2); //

    => [2, 4, 6] nums.map((x) => { x * 2; }); // => [undefined…]
  8. var nums = [1,2,3]; nums.map((x) => x * 2); //

    => [2, 4, 6] nums.map((x) => { return x * 2; }); // => [2, 4, 6]
  9. class Person { constructor(name, age) { this.name = name, this.age

    = age } about() { log(this.name, this.age);
 } };
  10. class Person { constructor(name, age) { this.name = name, this.age

    = age } about() { log(this.name, this.age);
 } };
  11. class Person { constructor(name, age) { this.name = name, this.age

    = age } about() { log(this.name, this.age);
 } };
  12. class Son extends Person { constructor(name, age) { super(name, age);

    this.son = true; } }; var jack = new Son('jack', 22); jack.about(); //=> jack 22 jack.son; //=> true
  13. class Son extends Person { constructor(name, age) { super(name, age);

    this.son = true; } }; var jack = new Son('jack', 22); jack.about(); //=> jack 22 jack.son; //=> true
  14. class Son extends Person { constructor(name, age) { super(name, age);

    this.son = true; } }; var jack = new Son('jack', 22); jack.about(); //=> jack 22 jack.son; //=> true
  15. class Son extends Person { constructor(name, age) { super(name, age);

    this.son = true; } }; var jack = new Son('jack', 22); jack.about(); //=> jack 22 jack.son; //=> true
  16. class Son extends Person { constructor(name, age) { super(name, age);

    this.son = true; } }; var jack = new Son('jack', 22); jack.about(); //=> jack 22 jack.son; //=> true
  17. var jack = { name: 'jack', age: 22, about() {

    log(this.name, this.age);
 } };
  18. var jack = { ['hello_' + (() => 'world')()]: 42

    }; console.log(jack.hello_world); // 42
  19. var name = 'jack'; var age = 22; log(name +

    ' is ' + age + 'years old'); // this is the worst
  20. var name = 'jack'; var age = 22; `${name} is

    ${age} years old` // rejoice
  21. function getPerson() { var name = 'jack'; var age =

    22; return { name, age };
 }; var {name, age} = getPerson();
  22. function getInfo(print=false) { if(print) { log(this.name, this.age);
 } else {

    return `${this.name} ${this.age}`
 }
 } getInfo();
  23. function total(x, y, z) { log(x + y + z);

    }; total.apply(null, [1, 2, 3]); total(...[1, 2, 3]);
  24. function total(x, y, z) { log(x + y + z);

    }; total.apply(null, [1, 2, 3]); total(...[1, 2, 3]);
  25. foo = 2; var fad = 2; function() { bar

    = 3; var baz = 4;
 } foo: 2, fad: 2, bar: 3 baz: 4
  26. function() { if(x) { var foo = 3; } var

    baz = 4;
 } foo: 3, baz: 4
  27. foo = 2; function() { var baz = 4; if(x)

    { let y = 2;
 }
 } foo: 2 baz: 4 y: 2
  28. foo = 2; function() { var baz = 4; if(x)

    { var z = 3; let y = 2;
 }
 } foo: 2 baz: 4, z: 3 y: 2
  29. var foo = 2; var bar = 3; export {foo,

    bar}; import {foo} from 'app' console.log(foo); // 2 app.js foo.js
  30. export default function() { return 2; }; import foo from

    'app' console.log(foo()); // 2 app.js foo.js
  31. var prom = new Promise( function(resolve, reject) { // do

    stuff if(everythingOK) { resolve('all worked!');
 } else { reject(Error('oh no'));
 }
 } );
  32. prom.then(function(resp) { // got here if stuff was OK
 },

    function(err) { // got here if stuff broke
 });