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

ES2015 and beyond

ES2015 and beyond

An introduction to the new features of JavaScript ES2015

Gabriele Petronella

December 01, 2015
Tweet

More Decks by Gabriele Petronella

Other Decks in Programming

Transcript

  1. TERMINOLOGY ECMASCRIPT (ES) sounds like a skin desease, it's actually

    a standard JAVASCRIPT (JS) a language that implements the said standard
  2. POP QUIZ! var b = 4; a = 38 +

    b; var a; console.log(a);
  3. POP QUIZ 2! var letters = ['a','b','c']; for (var i

    = 0; i < letters.length; i++ ) { var letter = letters[i]; setTimeout(function() { console.log(letter) }, 1000 * i); }
  4. var x = { bar: function(cb) { cb(); } }

    var y = { foo: function() { console.log(this); // <----- this x.bar(function() { // != console.log(this); // <----- this }); } } y.foo();
  5. var y = { foo: function() { console.log(this); var self

    = this; // <---- self is a way cooler name x.bar(function() { console.log(self); }); } }
  6. A SIMPLE RULE Whenever a function is called, we must

    look at the immediate left side of the brackets / parentheses “()”. If on the left side of the parentheses we can see a reference, then the value of “this” passed to the function call is exactly of which that object belongs to, otherwise it is the global object. HTTP://DAVIDSHARIFF.COM/BLOG/JAVASCRIPT-THIS-KEYWORD/
  7. var bigRoom = { size: 'big', logSize: function () {

    console.log(this.size); } }; var smallRoom = { size: 'small' }; smallRoom.logSize = bigRoom.logSize; bigRoom.logSize(); // ?? smallRoom.logSize(); // ??
  8. BLOCK SCOPING WITH LET for (let i = 0; i

    < letters.length; i++ ) { let letter = letters[i]; setTimeout(function() { console.log(letter) // a b c }, 1000 * i); }
  9. this BINDING WITH => let y = { foo: function()

    { console.log(this); // <--- this x.bar(() => { // = console.log(this); // <--- this }); } }
  10. BONUS: SYNTAX! [1, 2, 3].map(x => x + 2); -

    implicit return (if single expression) - no parenthesis (if single argument) Equivalent to [1, 2, 3].map(function (x) { return x + 2; });