Slide 1

Slide 1 text

ES6 UNCENSORED ____________ ____________ @angustweets

Slide 2

Slide 2 text

And on the 6th day, God created an abundance of Talking Animals, that they may be used in JavaScript inheritance examples.

Slide 3

Slide 3 text

The draft ES6 standard is scheduled for completion in early 2014

Slide 4

Slide 4 text

As soon as it comes out...

Slide 5

Slide 5 text

The Fun Police will emerge

Slide 6

Slide 6 text

...and tell you how to write it

Slide 7

Slide 7 text

...or rather how not to write it

Slide 8

Slide 8 text

until then...

Slide 9

Slide 9 text

go wild with ES6

Slide 10

Slide 10 text

If Hemingway wrote JavaScript

Slide 11

Slide 11 text

function fibonacci(size) { var first = 0, second = 1, next, count = 2; var result = [first, second]; if(size < 2) return "the request was made but it was not good" while(count++ < size) { next = first + second; first = second; second = next; result.push(next); } return result; }

Slide 12

Slide 12 text

If Kerouac wrote JavaScript

Slide 13

Slide 13 text

/*...the only numbers for me are the mad ones, take forty-three like a steam engine with a talky caboose at the end*/ n = 43, /*and that lanky fellow in a cocked fedora*/ r = 1 /*then back to our number, our mad number, mad to become one*/ while (n > 1) /*mad to descend*/ n--, /*mad to multiply*/ r = r * n /*and at the end, you see the blue center-light pop, and everybody goes 1.4050061177528801e+51...*/ r

Slide 14

Slide 14 text

ES6 If ES6 wrote JavaScript

Slide 15

Slide 15 text

GENERATORS ARROWS SETS REST DEFAULTS SPREAD PROXIES DESTRUCTURE ASSIGNMENT LET WEAK MAPS M O D ULES E S 6

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

GENERATORS ARROWS SETS REST DEFAULTS SPREAD PROXIES DESTRUCTURE ASSIGNMENT LET WEAK MAPS M O D ULES E S 6

Slide 18

Slide 18 text

1. Switch two Variables

Slide 19

Slide 19 text

destructuring //es5 var temp = a; a = b; b = temp; //es6 [a, b] = [b, a]

Slide 20

Slide 20 text

2. Find the Biggest Number in an Array

Slide 21

Slide 21 text

//es5 Math.max.apply(null, array); //es6 Math.max(...array); spread operator

Slide 22

Slide 22 text

3. Sum the Arguments

Slide 23

Slide 23 text

//es5 var sum = function(/* numbers */) { var asArray = [].slice.call(numbers); return asArray.reduce(function(a, b) { return a + b; }); }

Slide 24

Slide 24 text

//es6 let sum = (...numbers) => numbers.reduce((a, b) => a + b); rest operator arrow function

Slide 25

Slide 25 text

4. Remove the Duplicates from an Array

Slide 26

Slide 26 text

//es5 (efficient but type issues) function unique(arr) { var r = {}; arr.forEach(function(e){ r[e] = 1; }); return Object.keys(r); }

Slide 27

Slide 27 text

//es5 (works for all types bad ordering) function unique(arr) { var r = []; arr.forEach(function(e, i){ arr.indexOf(e, i+1) > -1 || r.push(e); }); return r; }

Slide 28

Slide 28 text

//es6 function unique(arr) { return [...Set(arr)]; } spread operator sets

Slide 29

Slide 29 text

5. Generate the Fibonacci Numbers

Slide 30

Slide 30 text

//es5 function fibonacci(size) { var a = 0, b = 1; var next, x = 2, result = [a, b]; while(x++ < size) { next = a + b; a = b; b = next; result.push(next); } return result; }

Slide 31

Slide 31 text

//es6 function fibonacci(size) { var a = 0, b = 1, x = 2, r=[a, b]; while(x++ < size) { [a, b] = [b, a+b]; r.push(b); } return r } destructuring

Slide 32

Slide 32 text

//es6 with generators function fibonacci(){ var a = 1, b = 1; while (true) { [a, b] = [b, a+b]; yield b; } } var generator = fibonacci(); generator.next(); //1 generator.next(); //2 destructuring generators

Slide 33

Slide 33 text

6. Factorial (Ramunujan’s Approximation)

Slide 34

Slide 34 text

//es5 function factorial(n){ var r = Math.sqrt(Math.PI) * Math.pow(n/Math.E,n); r *= Math.pow(Math.pow(4*(n*n) + n + 1/30, 1/6); return r; }

Slide 35

Slide 35 text

//es5 + with function factorial(n){ with(Math) { var r = sqrt(PI)*pow(n/E,n); r *= pow(8*pow(n, 3) + 4*(n*n) + n + 1/30, 1/6); return r; } }

Slide 36

Slide 36 text

//es6 function factorial(n) { var {sqrt, PI, pow, E} = Math; var r = sqrt(PI)*pow(n/E,n); r *= pow(8*pow(n, 3) + 4*(n*n) + n + 1/30, 1/6); return r; } destructuring

Slide 37

Slide 37 text

7. Sum the Squares

Slide 38

Slide 38 text

//es5 arr.map(function(number) { return number * number; }).reduce(function(a, b) { return a + b; });

Slide 39

Slide 39 text

//es6 [for (x of array) x*x].reduce((a,b)=>a+b); comprehesions arrow function

Slide 40

Slide 40 text

8. Module Pattern (ID Generator)

Slide 41

Slide 41 text

//es5 var idGenerator = function(id) { id || (id = 0); return function() { return id++; } } var nextFrom1000 = idGenerator(1000); nextFrom1000(); //1000 nextFrom1000(); //1001

Slide 42

Slide 42 text

//es6 let idGenerator= (id=0)=> ()=> id++; let nextFrom1000 = idGenerator(1000); nextFrom1000(); //1000 nextFrom1000(); //1001 defaults arrow function

Slide 43

Slide 43 text

9. Module Pattern (ID Generator 2)

Slide 44

Slide 44 text

//es5 var idGenerator = function(id) { id || (id = 0); function resetId() { return id = 0; } function nextId() { return id++; } return { nextId: nextId, resetId: resetId }; }

Slide 45

Slide 45 text

//es6 let idGenerator=(id=0)=> (resetId = ()=> id=0, nextId = ()=> id++, {resetId, nextId}); defaults arrow function literal shorthand

Slide 46

Slide 46 text

10. Resolve the Options

Slide 47

Slide 47 text

//es5 function echoABC(options) { options || (options = {}); console.log( options.a || 3, options.b || 4, options.c || 5); }

Slide 48

Slide 48 text

//es6 function echoABC({a=3, b=4, c=5}) { console.log(a, b, c); } destructuring defaults

Slide 49

Slide 49 text

11. Support Negative Array Indexes

Slide 50

Slide 50 text

//es5 var arr = [1, 2, 3]; //last item arr[-1];

Slide 51

Slide 51 text

//es5 var arr = [1, 2, 3]; //last item arr[-1]; //@#$%$#@

Slide 52

Slide 52 text

//es5 var arr = [1, 2, 3]; //last item arr[-1]; //@#$%$#@ arr[arr.length - 1]; //3

Slide 53

Slide 53 text

//es6 var arr = makeSmartArray([1,2,3]); arr[0]; //1 arr[-1]; proxies

Slide 54

Slide 54 text

//es6 var arr = makeSmartArray([1,2,3]); arr[0]; //1 arr[-1]; //3 proxies

Slide 55

Slide 55 text

//es6 var arr = makeSmartArray([1,2,3]); arr[0]; //1 arr[-1]; //3 arr.push(4); arr[-2]; //3 proxies

Slide 56

Slide 56 text

//es6 function makeSmartArray(arr) { return Proxy( arr, {get: function(p,x) { if (Number(x) < 0) { return p[p.length + Number(x)]; } return p[x]; }} ); } proxies

Slide 57

Slide 57 text

12. Null-safe Property Chains

Slide 58

Slide 58 text

//es5 var contactLastName = sale && sale.person && sale.person.name && sale.person.name.last;

Slide 59

Slide 59 text

//es5 //tobyho.com/2010/11/30/two-strategies-for-avoiding var contactLastName = NullSafe(sale). _('person'). _('name'). _('last').val() || "Unknown"

Slide 60

Slide 60 text

//es5 NullSafe function //tobyho.com/2010/11/30/two-strategies-for-avoiding function NullSafe(obj){ function NullSafe(obj){ this.obj = obj } Wrapper = NullSafe var proto = Wrapper.prototype proto.call = proto.$ = function(){ var name = arguments[0] var args = Array.prototype.slice.call(arguments, 1) var obj = null if (this.obj){ var func = this.obj[name] if (this.obj && func){ obj = func.apply(this.obj, args) } } return new Wrapper(obj) } proto.get = proto._ = function(){ var curr = this.obj for (var i = 0; i < arguments.length; i++){ if (curr != null){ curr = curr[arguments[i]] } } return new Wrapper(curr) } proto.val = function(){ return this.obj } proto.toString = function(){ return String(this.obj) } return new Wrapper(obj || this) }

Slide 61

Slide 61 text

//es6 nullProxy var Obj = Function(); var nullProxy = Proxy( {toString: () => 'nullProxy'}, { get: (t,p) => t[p] || new Obj() } ); Obj.prototype = nullProxy; proxies

Slide 62

Slide 62 text

//es6 var sale = Object.create(nullProxy /*, data*/); contactLastName = sale.contact.name.last; contactLastName; //'nullProxy' proxies

Slide 63

Slide 63 text

Where to Play with ES6...

Slide 64

Slide 64 text

firefox chrome (chrome://flags ‘enable experimental Javascript’) node --harmony traceur continuum github.com/paulmillr/es6-shim/

Slide 65

Slide 65 text

esdiscuss.org tc39wiki.calculist.org/es6/ people.mozilla.org/~jorendorff/es6-draft.html some resources... es6 standard... @esdiscuss on twitter compatibility... kangax.github.io/es5-compat-table/es6/

Slide 66

Slide 66 text

@angustweets FIN UNCENSORED ____________ ____________

Slide 67

Slide 67 text

Cheetah cubs: http://www.bhmpics.com/view-cheetah_cubs_playing-1920x1080.html ES6 robot limbs: http://www.tfs.rcs.k12.tn.us/TEACHERS/halimir/index.html Image credits ES6opoly is based on Monopoly by Hasbro Inc.