1. I want my ES6, like ES.NOWI want my ES6, like ES.NOW
Ken Rimple Mentor, Trainer, Consultant Chariot
Solutions
Slides - <-- that's not a zerohttp://1drv.ms/1DJQ4RO
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
1 of 43 04/10/2015 06:04 PM
2. TopicsTopics
What is ES6?
Where are we now?
How can we code ES6 now and run in ES5?
Client-side
Server-side
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
2 of 43 04/10/2015 06:04 PM
3. What is ES6?What is ES6?
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
3 of 43 04/10/2015 06:04 PM
4. ECMASCRIPT 6ECMASCRIPT 6
The next evolution of JavaScript (ECMASCRIPT === ES)
Being embedded in browsers within the next 18
months
Lots of new features
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
4 of 43 04/10/2015 06:04 PM
5. Major new featuresMajor new features
Classes, extends, constructors and member functions
class Klingon extends Warrior {
constructor(name) { }
methodA();
methodB();
}
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
5 of 43 04/10/2015 06:04 PM
6. Local variables, constantsLocal variables, constants
function foo() {
// function-scoped
var x = 234;
if (x === 234) {
let b = 24; // scoped to if
}
}
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
6 of 43 04/10/2015 06:04 PM
7. Arrow functions and string magicArrow functions and string magic
myArray.forEach((val) =>
console.log(`${val.a} - ${val.b}`);
var longStr = `
This is a long
string with
multiple lines`;
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
7 of 43 04/10/2015 06:04 PM
8. Default and "rest" parameters to functionsDefault and "rest" parameters to functions
function foo(a = 234, ...b) {
console.log(a);
for (ele of b) {
console.log(ele);
}
}
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
8 of 43 04/10/2015 06:04 PM
9. DestructuringDestructuring
Pull out parts of an object into variables
let a = { b: 'c', d: 'e' };
let {b, d} = a;
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
9 of 43 04/10/2015 06:04 PM
10. Better collectionsBetter collections
let keys = new Set();
keys.add(1); keys.add(2);
keys.has(3) // false
keys.has(1) // true
let map = new Map();
map.set('a', 123);
map.set('b', 'foobarbaz');
map.has('a') // true
map.get('a') // 123
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
10 of 43 04/10/2015 06:04 PM
11. Promises (native to JS!)Promises (native to JS!)
Called API:
return new Promise((resolve, reject) => {
resolve(answer);
// error condition
reject(errorData);
}
Caller:
apiCall.then(
(answer) => { ... },
(error) => { ... }
});
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
11 of 43 04/10/2015 06:04 PM
12. GeneratorsGenerators
function *shoppingList() {
yield 'apples';
yield 'oranges';
yield 'cereal';
}
let iterator = shoppingList();
while(true) {
var obj = iterator.next();
if (obj.done) break;
console.log(obj.value);
}
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
12 of 43 04/10/2015 06:04 PM
13. What is supported now?What is supported now?
See
Browser-side - not everything natively - unless
you're on Firefox Nightly
Or you can use transpilers, shims and/or a build
process
Server-side - depends on engine
http://kangax.github.io/compat-table/es6/
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
13 of 43 04/10/2015 06:04 PM
14. ES6 in the Browser...ES6 in the Browser...
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
14 of 43 04/10/2015 06:04 PM
15. What about today?What about today?
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
15 of 43 04/10/2015 06:04 PM
16. Let's move the enterprise to that NOW!Let's move the enterprise to that NOW!
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
16 of 43 04/10/2015 06:04 PM
17. How else? Two major themesHow else? Two major themes
Dynamic run-time translation
Transpiling (translation compiling) to generate source
(or source + library references)
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
17 of 43 04/10/2015 06:04 PM
19. BabelBabel
Has many flags and options
Very configurable
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
19 of 43 04/10/2015 06:04 PM
20. Babel interactive APIBabel interactive API
< script src="lib/browser.js"></ script>
< script src="lib/browser-polyfill.js"></ script>
< script src="lib/shim.js"></ script>
Now you can mount scripts with type of text/babel
and they will be transpiled
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
20 of 43 04/10/2015 06:04 PM
21. Approach not quite recommended...Approach not quite recommended...
Babel is meant to be run as part of a build
Not all features work in a dynamic mode (modules,
etc.)
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
21 of 43 04/10/2015 06:04 PM
22. Babel command lineBabel command line
$ npm install -g babel
$ babel foo.js
// output returned inline
Better yet, you should use a build system
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
22 of 43 04/10/2015 06:04 PM
23. Build optionsBuild options
Build-based transformer plugin (gulp-babel)
Browserify with babel plugin
Karma with pre-processor
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
23 of 43 04/10/2015 06:04 PM
24. Babel Gulp project demoBabel Gulp project demo
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
24 of 43 04/10/2015 06:04 PM
25. Code transformers supportedCode transformers supported
Use --help to return list of transformers
$ babel --help
Transformers:
- [asyncToGenerator]
- [bluebirdCoroutines]
- es3.memberExpressionLiterals
- es3.propertyLiterals
- es5.properties.mutators
...
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
25 of 43 04/10/2015 06:04 PM
26. Adding or removing transformersAdding or removing transformers
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
26 of 43 04/10/2015 06:04 PM
27. Babel has a set of default and optional transformers
Blacklist normally included transformers to remove
Whitelist - only these transformers are executed
Concept - remove transpiler features as browsers
support them - reality? Maybe not or complex build
targeting each browser supported...
babel --blacklist=es6.spread,
es6.regex.unicode foo.js
babel --whitelist=es6.classes,es6.forOf
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
27 of 43 04/10/2015 06:04 PM
28. TraceurTraceur
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
28 of 43 04/10/2015 06:04 PM
29. Using Traceur in the browserUsing Traceur in the browser
< script src=".../traceur.js">< /script>
< script src=".../bootstrap.js">< /script>
< script type="module">
Your ES6 code here...
< /script>
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
29 of 43 04/10/2015 06:04 PM
30. Traceur in a buildTraceur in a build
You can use
tracer as a command-line utility
traceur-gulp (|grunt|whatever)
6to5ify with Browserify
Karma traceur preprocessor
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
30 of 43 04/10/2015 06:04 PM
31. Some challengesSome challenges
Generated ES5 code is not always correct
The transpiler cannot know all API changes by itself
Example - Array methods
from is an ES6 feature in Array, but was not included
in the transpiler directly.
Babel provides a polyfill for this - polyfill.js - in
the library
Array.from({length: 5}, (v, k) => k);
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
31 of 43 04/10/2015 06:04 PM
32. Transpiling is not a panaceaTranspiling is not a panacea
You need to pick a module loader strategy to transpile
down to
You need to re-test once browsers begin to support
features
Some ES6 features (tail-recursion) will require binary
browser changes
Some APIs won't be available without polyfills
Check sites like Kangax
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
32 of 43 04/10/2015 06:04 PM
33. Where are we heading?Where are we heading?
Angular 2.0 - will use the traceur compiler + AtScript
(===> Typescript 1.5)
Typescript 1.5 - will generate ES5 AND ES6, contain
AtScript annotationes
Ember 2 - will use ES6 coding features (with Babel)
EVENTUALLY - ES6 rules them all
But we thought that with HTML5 too
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
33 of 43 04/10/2015 06:04 PM
34. Server-side ECMAScript 6Server-side ECMAScript 6
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
34 of 43 04/10/2015 06:04 PM
35. Several Options hereSeveral Options here
NodeJS with a transpiler to ES5 (see prior examples)
NodeJS with a wrapper such as node-babel
NodeJS activating Harmony features directly
io.js (fork of NodeJS)
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
35 of 43 04/10/2015 06:04 PM
36. NodeJS and ECMAScript 6 features w/BabelNodeJS and ECMAScript 6 features w/Babel
Just install Babel or
Run node-babel
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
36 of 43 04/10/2015 06:04 PM
37. NodeJS and ECMAScript 6NodeJS and ECMAScript 6
You can enable ES6 features
Much of ES6 is implemented
You must turn on special flags
$ node --v8-options | grep "harmony"
--harmony_scoping (enable harmony block scoping)
--harmony_modules (enable harmony modules
(implies block scoping))
--harmony_generators (enable harmony generators)
--harmony_strings (enable harmony string)
--harmony_arrays (enable harmony arrays)
...
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
37 of 43 04/10/2015 06:04 PM
38. WelcomeWelcome babel-nodebabel-node
Runs Node with a Babel shim
babel-node
> myfunc = () => console.log('hiya!');
> myfunc();
hiya!
All ES6 harmony flags supported as of 5.0.8
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
38 of 43 04/10/2015 06:04 PM
39. ES6 Node server exampleES6 Node server example
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
39 of 43 04/10/2015 06:04 PM
40. Alternative to NodeJs - io.jsAlternative to NodeJs - io.js
A fork of node.js
Updated status https://github.com/iojs/io.js
/issues/1336
Controversial, but has a number of developers focused
on moving the platform forward
Already supports a number of ES6 features out of the
box, can turn on the rest
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
40 of 43 04/10/2015 06:04 PM
41. ES6-ready framework exampleES6-ready framework example
Koa - developer of Express - uses --harmony flag
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
41 of 43 04/10/2015 06:04 PM
42. Should you do this now?Should you do this now?
Up to you
Specs will be tweaked
Do NOT dive in and use every feature without
weighing it
You can still code "Good Parts" style
But Crockford has a video on the newer "Good Parts"
of ES6
Link: http://goo.gl/eD4UB5
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
42 of 43 04/10/2015 06:04 PM
43. ResourcesResources
Traceur -
Babel -
Slides - <-- that's not a zero
https://github.com/google/traceur-compiler
https://babeljs.io
http://1drv.ms/1DJQ4RO
reveal.js - The HTML Presentation Framework http://0.0.0.0:8000/?print-pdf#/
43 of 43 04/10/2015 06:04 PM