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

Say my name!

Say my name!

A deeply analysis on the modern JavaScript (ES6 - ES2015/ES7 - ES2016).
JSConfUY 2015

Jaydson Gomes

April 25, 2015
Tweet

More Decks by Jaydson Gomes

Other Decks in Programming

Transcript

  1. JSConfUY 2015 Say my name! A deeply analysis on the

    modern JavaScript (ES6 - ES2015/ES7 - ES2016)
  2. ES6 support (April 2015) 73% 69% 62% 45% 74% 23%

    41% 20% http://kangax.github.io/compat-table/es6/
  3. String.includes(txt, start) // String.contains(txt, start) let msg = "JS Conf

    UY!"; console.log(msg.includes("!")); // true console.log(msg.includes("JS")); // true console.log(msg.includes("X")); // false console.log(msg.includes("JS", 8)); // false
  4. // String.startsWith(txt, start) let msg = "JS Conf UY!"; console.log(msg.startsWith("JS"));

    // true console.log(msg.startsWith("js")); // false console.log(msg.startsWith("C")); // false console.log(msg.startsWith("C", 3)); // true
  5. // String.endsWith(txt, end) let msg = "JS Conf UY!"; console.log(msg.endsWith("!"));

    // true console.log(msg.endsWith("UY!")); // true console.log(msg.endsWith("Y!", 8)); // false console.log(msg.endsWith("Conf", 7)); // true
  6. // Template strings // Basic literal string creation console.log(`In JavaScript

    '\n' is a line-feed.`); // Multiline strings console.log(`In JavaScript this is not legal.`); // Construct a DOM query var name = "Bob", time = "today"; console.log(`Hello ${name}, how are you ${time}?`);
  7. // Template strings // Basic literal string creation console.log(`In JavaScript

    '\n' is a line-feed.`); // Multiline strings console.log(`In JavaScript this is not legal.`); // Construct a DOM query var name = "Bob", time = "today"; console.log(`Hello ${name}, how are you ${time}?`);
  8. // Template strings // Basic literal string creation console.log(`In JavaScript

    '\n' is a line-feed.`); // Multiline strings console.log(`In JavaScript this is not legal.`); // Construct a DOM query var name = "Bob", time = "today"; console.log(`Hello ${name}, how are you ${time}?`);
  9. // Octal and Binary Literals // ECMAScript 6 var value1

    = 0o71; // 57 in decimal var value2 = 0b101; // 5 in decimal Credits: Nicholas Zakas, Understanding ES6 https://github.com/nzakas/understandinges6
  10. Math.acosh(x) Math.asinh(x) Math.atanh(x) Math.cbrt(x) Math.clz32(x) Math.cosh(x) Math.expm1(x) Math.fround(x) Math.log1p(x) Math.log10(x)

    Math.log2(x) Math.sign(x) Math.sinh(x) Math.tanh(x) Math.trunc(x) Math.hypot(...values) Math.imul(x, y)
  11. // Destructuring assignment (array pattern) var m = 11, d

    = 29, y = 2014; let [m, d, y] = [11, 29, 2014];
  12. // Destructuring assignment (object pattern) function today() { return {

    d: 29, m: 11, y: 2014 }; } let { m: month, y: year } = today(); // month = 11, year = 2014
  13. // Block scope /* Let */ for (var i =

    0; i < 3; i++) { let j = i * i; console.log(j); // Works } console.log(j); // Fail http://jsrocks.com/2014/08/what-you-need-to-know-about-block-scope-let/
  14. // Single parameter arrow fn let plusOne = x =>

    x + 1; console.log( plusOne(5) ); // 6
  15. // Single parameter arrow fn let plusOne = x =>

    x + 1; console.log( plusOne(5) ); // 6 Function
  16. // Single parameter arrow fn let plusOne = x =>

    x + 1; console.log( plusOne(5) ); // 6 Param name
  17. // Single parameter arrow fn let plusOne = x =>

    x + 1; console.log( plusOne(5) ); // 6 return
  18. // n parameters arrow fn let sum = (n1, n2)

    => n1 + n2; console.log( sum(2,2) ); // 4
  19. // Parameterless Arrow fn let conf = () => "JSConfAR";

    console.log( conf() ); // JSConfAR
  20. // Arrow fn with a body let doSomething = (x,

    y) => { // logic return true; }
  21. // Classical ‘this’ issue var foo = { init: function

    () { setTimeout(function () { this.log(); }, 1000); }, log: function () { console.log('foooo'); } } foo.init(); //TypeError: this.log is not a function
  22. // Fixing ‘this’ issue ES5 way var foo = {

    init : function () { setTimeout((function () { this.log(); }).bind(this), 1000); }, log : function () { console.log('foooo'); } } foo.init(); // foooo
  23. // Fixing ‘this’ issue ES6 way let foo = {

    init : function () { setTimeout( () => this.log(), 1000 ); }, log : function () { console.log('foooo'); } }; foo.init(); // foooo
  24. class Animal { constructor(name) { this.name = name; } breathe

    () { console.log( `${this.name} is breathing` ); } }
  25. class Dog extends Animal { constructor(name) { super(name); } bark()

    { console.log(`Woof! ${this.name} is barking`); } }
  26. class Cat extends Animal { constructor(name) { super(name); } meow()

    { console.log(`Meow! ${this.name} is meowing`); } }
  27. // Creating modules // baz.js let baz = 'baz'; export

    default baz; // app.js import baz from “baz”;
  28. // Creating modules // print.js let print = function (what)

    { return `print module - ${what}`; } export default print;
  29. // Creating modules // foo.js import baz from "./baz"; console.log('From

    module baz >>> ', baz); let foo = 'foo'; export default foo; export let bar = 'bar'; // foo.js import { bar } from “foo”;
  30. // Using modules // app.js import print from "./modules/print"; import

    foo, { bar } from "./modules/foo"; console.log( print('it works') ); // print module - it works console.log( print('wow') ); // print module - wow console.log( foo ); // foo console.log( bar ); // bar