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

Embracing ES6

r31gN_
July 25, 2016

Embracing ES6

r31gN_

July 25, 2016
Tweet

More Decks by r31gN_

Other Decks in Technology

Transcript

  1. ES6 history • ES6 is also known as Harmony •

    The correct term however is ES2015 (naming convention established) • Was finalised in June 2015 • Future versions will follow the ES[YYYY] pattern
  2. ES6 release lifecycle • Yearly release schedule (yay) • Features

    that don’t make the cut take the next train • This release cycle will pressure browser vendors into quickly implementing new features
  3. So … what is it? • Simply … next JS

    version • An evolution of the language (syntax and functionality) • Minimises the initial gap for true OOP developers • Better, faster, more reliable development
  4. How to use it? • Browsers do not fully support

    ES6 features (though some are natively implemented) • You can still write ES6 code though • Use transpilers in your build process
  5. Transpilers • Transpilers are simply JS-to-JS tools • They transform

    your ES6 code (and unsupported features) to ES5 code • Some features can’t be exactly transformed (let, const) so transpilers fall back to old stuff - var
  6. Transpilers • Best transpiler ever: Babel • It has human-readable

    output • Babel also works server-side (for Node) • Babel has a thriving ecosystem that already supports some of ES2016 and has plugin support
  7. Transpilers • Use babel to transpile ES6 to ES5 for

    static builds • Use babelify with Gulp, Grunt or simple npm run (usually with browserify for require statements) • Use babel-node with any version of node, as it transpiles modules into ES5
  8. Let & const • Better alternatives to the var keyword

    • Let and const fix something that was confusing in JS - they employ block-level scoping • TDZ (temporal dead zone) - beginning of block, end where declaration happens
  9. Let & const • Declaration of the same variable (both

    with let and const) will fail • Const variables must be initialized • Assigning to const after initialization fails (silently or loudly depending on use strict)
  10. Assignment destructuring • A shorthand for pulling values out of

    objects or arrays • Supports defaults, aliases and nesting
  11. Spread operator & rest parameters • Used with … •

    Rest parameter is a better arguments • Spread operator helps avoid apply, or casts array-like collections to arrays (list of DOM nodes to array)
  12. Arrow functions • Terse way to declare functions • Depending

    on how you specify them, you might have implicit return • You can’t name them • Arrow functions are bound to their lexical scope (actually they don’t define a this, arguments, etc.)
  13. Template literals • Declared using ` (backtick) in addition to

    “, ‘ • They can be multiline • They allow interpolation of variables or valid JS expressions
  14. Object literals • Objects now have property value shorthand •

    You can also have computed property names • Method definitions in object literals also have an improved, more terse syntax
  15. Classes • Syntactic sugar on top of old prototypes •

    Supports inheritance (via extend keyword), constructors, super calls, static methods • They still rely on prototype inheritance (this is different than traditional OOP languages)
  16. Symbols • A new primitive type in ES6 (an awful

    lot like strings) • Symbols are immutable and unique • They have the type symbol
  17. Symbols • Symbols are not iterable (some exceptions - Symbol.

    iterator) • Can be as global as you want (cross-realm) • There are built-in symbols - Symbol.iterator, Symbol. match • Use them for name clashes (objects as hash maps) or for defining protocols (eg: iterator)
  18. Iterators • Iterators and the iterable protocol define how to

    iterate over any object • Uses the Symbol.iterator built-in symbol • Must return an object that has a next method (with value and done)
  19. Iterators • Object that have implemented the iterable protocol are

    iterable (you can use the for … of, spread operator, etc.) • Some built-ins are iterable by default (Arrays, Strings, arguments, NodeList, etc.) • Iterators are lazy (one item at a time)
  20. Generators • Generators are a special kind of iterator •

    Make use of the yield keyword • Generator function execution is suspended, remembering the last position • Useful to make async flows feel synchronous
  21. Promises • A way to deal with async code (in

    a sync-like manner) • Created by using the Promise “class” (will either be resolved or rejected) • Promises are chainable (then or catch - return new promises)
  22. Promises • Start out in pending state and are settled

    at a later time (resolved or rejected) • Promises can be settled only once • Only one of the then or catch branch (and sub-branches) will be executed • There is Promise.all and Promise.race
  23. Maps • A replacement to the common pattern of creating

    a hash-map using POJOs • Adheres to the iterable protocol • Keys can be arbitrary values (eg: DOM nodes, strings, functions, etc.) • Methods to get, set or check values
  24. WeakMaps • A subset of Map with limited functionality •

    It’s not iterable • Keys must be objects (does not support primitive types as keys)
  25. WeakMaps • WeakMap holds references to its keys weakly, meaning

    that if there are no other references to one of its keys, the object is subject to garbage collection • Use it when you need to specify object metadata or extend an object while still being able to garbage collect it if nobody else cares about it
  26. Sets • Quite similar to Map (iterable, same methods) •

    Sets don’t have keys, only values (the key is the value) • Sets can’t have duplicate values because the values are also used as keys
  27. WeakSets • Quite similar to WeakMap • Has only values

    (they act as keys also) • Not iterable, works only with objects
  28. Proxies • Used to augument behaviour of objects through traps

    (proxies act as a passthrough) • Your defined handlers determine the access rules to the underlying object • Can be revocable • A lot of traps: get, set, has, defineProperty, apply, ownKeys, etc.
  29. Numbers • Some cool additions were made to the Number

    class • Methods like isInteger, isSafeInteger or constants such as EPSILON, MAX_SAFE_INTEGER, MIN_SAFE_INTEGER, etc.
  30. Math • Some cool additions were made to the Math

    class • Methods like sine, trunc, cbrt, sinh, cosh, asinh, hypot, fround, etc.
  31. Arrays • Some cool additions were made to the Array

    class • Methods like Array.from, Array.of, copyWithin, fill, find, findIndex, etc.
  32. Objects • Some cool additions were made to the Object

    class • Methods like assign, is, getOwnPropertySymbols, setPrototypeOf, and object literals
  33. Strings • Some cool additions were made to the String

    class • Methods like startsWith, endsWith, repeat, include, codePointAt, fromCodePoint, etc.
  34. Modules • ES6 modules are files that export an API

    • You can export named or default bindings • You can export a list (multiple bindings)
  35. Modules • Import is done in CommonJS style (using the

    import keyword) • You can alias imports, import everything within a module, etc.
  36. That’s all folks! • ES6 (ES2015) is here to stay

    and evolve • Embrace it today (production as well)