Slide 1

Slide 1 text

...AND ES6 FOR ALL

Slide 2

Slide 2 text

Jaydson Gomes JavaScript Enthusiast Software Developer BrazilJS Conf Curator

Slide 3

Slide 3 text

Agenda

Slide 4

Slide 4 text

3. Arrow Fn 1. History 4. Classes 6. Template Strings 5. Parameters 7. Block Scope 8. Modules 9. Promises 10. Generators #Traceur #Harmonic #ES6Rocks #NodeJS #Shims 2. Basics

Slide 5

Slide 5 text

History

Slide 6

Slide 6 text

10 days in may 1995

Slide 7

Slide 7 text

Mocha LiveScript JavaScript

Slide 8

Slide 8 text

ECMAScript

Slide 9

Slide 9 text

ECMAScript (ECMA-262) is the name of the international standard that defines JavaScript. Credits: Allen Wirfs-Brock, ES6: A Better JavaScript for the Ambient Computing Era http://www.slideshare.net/allenwb/wdc14-allebwb

Slide 10

Slide 10 text

Credits: Ecmascript6 the future is here, https://speakerdeck. com/sebarmeli/ecmascript-6-the-future-

Slide 11

Slide 11 text

Credits: Ecmascript6 the future is here, https://speakerdeck. com/sebarmeli/ecmascript-6-the-future-

Slide 12

Slide 12 text

TC39

Slide 13

Slide 13 text

Credits: Allen Wirfs-Brock, ES6: A Better JavaScript for the Ambient Computing Era http://www.slideshare.net/allenwb/wdc14-allebwb

Slide 14

Slide 14 text

http://esdiscuss.org/ http://wiki.ecmascript.org/doku.php? id=harmony:specification_drafts https://people.mozilla. org/~jorendorff/es6-draft.html

Slide 15

Slide 15 text

The basics

Slide 16

Slide 16 text

Strings

Slide 17

Slide 17 text

var msg = "Hello world!"; console.log(msg.contains("o")); // true console.log(msg.contains("x")); // false console.log(msg.contains("o", 8)); // false String.contains(txt, start) Credits: Nicholas Zakas, Understanding ES6 https://github.com/nzakas/understandinges6

Slide 18

Slide 18 text

String.startsWith(txt, start) var msg = "Hello world!"; console.log(msg.startsWith("Hello")); // true console.log(msg.startsWith("o")); // false console.log(msg.startsWith("o", 4)); // true Credits: Nicholas Zakas, Understanding ES6 https://github.com/nzakas/understandinges6

Slide 19

Slide 19 text

String.endsWith(txt, start) var msg = "Hello world!"; console.log(msg.endsWith("!")); // true console.log(msg.endsWith("world!")); // true console.log(msg.endsWith("o", 8)); // true Credits: Nicholas Zakas, Understanding ES6 https://github.com/nzakas/understandinges6

Slide 20

Slide 20 text

String.repeat(number) console.log("x".repeat(3)); // "xxx" console.log("hello".repeat(2)); // "hellohello" console.log("abc".repeat(4)); // "abcabcabcabc" Credits: Nicholas Zakas, Understanding ES6 https://github.com/nzakas/understandinges6

Slide 21

Slide 21 text

Numbers

Slide 22

Slide 22 text

// ECMAScript 3 var number = 071; // 57 in decimal var value1 = parseInt("71"); // 71 var value2 = parseInt("071"); // 57 Octal and Binary Literals Credits: Nicholas Zakas, Understanding ES6 https://github.com/nzakas/understandinges6

Slide 23

Slide 23 text

// ECMAScript 5 var number = 071; // 57 in decimal var value1 = parseInt("71"); // 71 var value2 = parseInt("071"); // 71 var value3 = parseInt("071", 8); // 57 function getValue() { "use strict"; return 071; // syntax error } Octal and Binary Literals Credits: Nicholas Zakas, Understanding ES6 https://github.com/nzakas/understandinges6

Slide 24

Slide 24 text

// ECMAScript 6 var value1 = 0o71; // 57 in decimal var value2 = 0b101; // 5 in decimal Octal and Binary Literals Credits: Nicholas Zakas, Understanding ES6 https://github.com/nzakas/understandinges6

Slide 25

Slide 25 text

console.log(isFinite(25)); // true console.log(isFinite("25")); // true console.log(Number.isFinite(25)); // true console.log(Number.isFinite("25")); // false console.log(isNaN(NaN)); // true console.log(isNaN("NaN")); // true console.log(Number.isNaN(NaN)); // true console.log(Number.isNaN("NaN")); // false isFinite() and isNaN() Credits: Nicholas Zakas, Understanding ES6 https://github.com/nzakas/understandinges6

Slide 26

Slide 26 text

console.log(Number.isInteger(25)); // true console.log(Number.isInteger(25.0)); // true console.log(Number.isInteger(25.1)); // false Number.isInteger() Credits: Nicholas Zakas, Understanding ES6 https://github.com/nzakas/understandinges6

Slide 27

Slide 27 text

console.log(Math.pow(2, 53)); // 9007199254740992 console.log(Math.pow(2, 53) + 1); // 9007199254740992 var inside = Number.MAX_SAFE_INTEGER, outside = inside + 1; console.log(Number.isInteger(inside)); // true console.log(Number.isSafeInteger(inside)); // true console.log(Number.isInteger(outside)); // true console.log(Number.isSafeInteger(outside)); // false Safe integers -2^53 & 2^53 Credits: Nicholas Zakas, Understanding ES6 https://github.com/nzakas/understandinges6

Slide 28

Slide 28 text

Math

Slide 29

Slide 29 text

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)

Slide 30

Slide 30 text

Arrow FN

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

=> Lexical this binding => Not newable => Can't change this => No arguments object Arrow Functions

Slide 33

Slide 33 text

let plus = x => x + 1; console.log( plus(2) ); // 3 Arrow Fn one parameter

Slide 34

Slide 34 text

let sum = (num1, num2) => num1 + num2; console.log( sum(2,3) ); // 5 Arrow Fn n parameters

Slide 35

Slide 35 text

let confName = () => "Front in Aracaju"; console.log( confName() ); //Front in Aracaju Arrow Fn no parameters

Slide 36

Slide 36 text

let doSomething = (x, y) => { // Some logic return true; }; Arrow Fn body

Slide 37

Slide 37 text

var foo = { init: function () { setTimeout(function () { this.log(); }, 1000); }, log: function () { console.log('foooo'); } } foo.init(); //undefined is not a function classical ‘this’ error

Slide 38

Slide 38 text

var foo = { init : function () { setTimeout((function () { this.log(); }).bind(this), 1000); }, log : function () { console.log('foooo'); } } foo.init(); // foooo Fixing ‘this’ error ES5 way

Slide 39

Slide 39 text

var foo = { init : function () { setTimeout( () => this.log(), 1000); }, log : function () { console.log('foooo'); } }; foo.init(); // foooo Fixing ‘this’ error ES6 way

Slide 40

Slide 40 text

Classes

Slide 41

Slide 41 text

class Animal { constructor(name) { this.name = name; } breathe () { console.log( `${this.name} is breathing` ); } }

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

Template Strings

Slide 45

Slide 45 text

// 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}?`);

Slide 46

Slide 46 text

Paramaters

Slide 47

Slide 47 text

/* Rest parameters */ function sortRestArgs(...theArgs) { var sortedArgs = theArgs.sort(); return sortedArgs; } console.log(sortRestArgs(10,4,1,2,3)); // 1,10,2,3,4

Slide 48

Slide 48 text

/* Spread parameters */ function f(x, y, z) { console.log(x,y,z); } var args = [0, 1, 2]; f(...args); // 0 1 2 /* Default paramaters */ function foo(bar='baz') { console.log(bar); } foo(); // baz

Slide 49

Slide 49 text

Scope Block

Slide 50

Slide 50 text

/* Let */ for (var i = 0; i < 3; i++) { let j = i * i; console.log(j); // Works } console.log(j); // Fail /* Const */ const π = 3.14; console.log(π); try { π += 1; } catch (constError) { console.log(constError); }

Slide 51

Slide 51 text

Modules

Slide 52

Slide 52 text

var asap; var isNode = typeof process !== "undefined" && {}.toString.call(process) === "[object process]"; if (isNode) { asap = process.nextTick; } else if (typeof setImmediate !== "undefined") { asap = setImmediate; } else { asap = setTimeout; } export default asap; Creating a module Credits: JSModules.io http://jsmodules.io/

Slide 53

Slide 53 text

import asap from "asap"; asap(function() { console.log("hello async world!"); }); Importing a module Credits: JSModules.io http://jsmodules.io/

Slide 54

Slide 54 text

var asap; var isNode = typeof process !== "undefined" && {}.toString.call(process) === "[object process]"; if (isNode) { asap = process.nextTick; } else if (typeof setImmediate !== "undefined") { asap = setImmediate; } else { asap = setTimeout; } export default asap; export var later = isNode ? process.setImmediate : asap; Named exports Credits: JSModules.io http://jsmodules.io/

Slide 55

Slide 55 text

import { later } from "asap"; later(function() { console.log("Running after other network events"); }); import asap, { later } from "asap"; Named imports Credits: JSModules.io http://jsmodules.io/

Slide 56

Slide 56 text

import { unlink as rm } from "fs"; // Rename import * as fs from "fs"; // Into namespace Conveniences Credits: JSModules.io http://jsmodules.io/

Slide 57

Slide 57 text

Promises

Slide 58

Slide 58 text

let confs = ['Aracaju','Poa','Sampa','Rio']; let FrontIn = function() { return new Promise(function(resolve, reject) { setTimeout(() => { let len = confs.length; let label = 'FrontIn'; let rand = Math.floor(Math.random() * len); let conf = `${label} ${confs[rand]}`; resolve(conf); }, 1000); }); }; Promises

Slide 59

Slide 59 text

FrontIn().then(function(conf) { console.log(conf); }); Promises http://www.es6fiddle.net/hy9dtwxv/

Slide 60

Slide 60 text

Promise.all([ FrontIn(), FrontIn(), FrontIn(), FrontIn() ]).then(function (data) { console.log(data); }); Promises https://speakerdeck.com/jcemer/controle- de-fluxo-com-execucao-assincrona

Slide 61

Slide 61 text

Generators

Slide 62

Slide 62 text

From MDN: “Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances” https://developer.mozilla.org/en- US/docs/Web/JavaScript/Reference/Statement s/function*

Slide 63

Slide 63 text

function* idMaker() { var index = 0; while(true) yield index++; } var gen = idMaker(); console.log(gen.next().value); // 0 console.log(gen.next().value); // 1 console.log(gen.next().value); // 2 // ...

Slide 64

Slide 64 text

Node.JS

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

#!/usr/bin/env node require('v8-argv')('--harmony', __dirname + '/bin/cli/program'); https://github.com/logicalparadox/v8-argv

Slide 67

Slide 67 text

Shim https://github.com/paulmillr/es6-shim/

Slide 68

Slide 68 text

Traceur

Slide 69

Slide 69 text

➔ Array Comprehension ➔ Arrow Functions ➔ Classes ➔ Computed Property Names ➔ Default Parameters ➔ Destructuring Assignment ➔ Iterators and For Of ➔ Generator Comprehension ➔ Generators ➔ Modules ➔ Numeric Literals ➔ Property Method Assignment ➔ Object Initializer Shorthand ➔ Rest Parameters ➔ Spread ➔ Template Literals ➔ Promises ➔ Block Scoped Binding ➔ Symbols

Slide 70

Slide 70 text

Demo

Slide 71

Slide 71 text

http://es6rocks.com/

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

https://github.com/es6rocks/harmonic

Slide 74

Slide 74 text

Contribute

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

Questions?