ECMAScript 6

A PRIMER

While this presentation is largely technical, this is also good information for sales, marketing and UX. The onset of ES6 in the market will certainly create new opportunities with new and existing clients.

What is ECMA?

International standards based organization with a private membership. In addition to ECMAScript, manages standards for C#, OpenXML, NFC, CLI and others. Based in Geneva, Switzerland.

www.ecma-international.org

JavaScript falls under the ECMA-262 Standard

History of ECMAScript

  • 1997 First edition

  • 1998 Second edition

  • 1999 Third edition

  • 2008 Fourth edition

  • 2009 Fifth edition

  • 2011 Fifth edition (5.1)

  • 2015 Sixth edition (Rev 36, RC3 as of 3/17/15)

Over 40 major new features in ES6. Including sub-features, the count is well over 100

The draft specification contains the full list

Default Parameters

function logDefaults(a = 1, b = 2) { alert(a); } logDefaults();

A New Scope

function fnScoped() { if (true) { // x is "hoisted" to the function block var x = 'It\'s true'; } if (true) { // x should be undefined... or should it? alert(x) } } fnScoped();

Block Scope (let)

function blockScoped() { if(true) { let x = 'it\'s true'; alert(x); } try { alert(x); } catch(e) { alert('x is undefined'); } } blockScoped();

Block Scope (const)

// const are immutable const foo = "bazinga!"; alert(foo); //force a compiler exception foo = "bar";

Template Strings (interpolation)

let piecesPerPie = 6; let numberOfPies = 3; alert(`There are ${numberOfPies} pies, so we have ${piecesPerPie * numberOfPies} pieces.`);

Fat Arrow

'use strict'; let gen = (seed) => seed*Math.random(); alert(gen(1000));

Object Destructuring

//syntactic sugar let person = { givenName: 'Tom', surName: 'Cornilliac' } let{givenName:first, surName:last} = person; alert(`My name is ${first} ${last}`);

Classes

class Address { constructor( city, state ) { this.city = city; this.state = state; } toString() { return `${this.city}, ${this.state}`; } } alert( new Address( 'Marlborough', 'MA' ).toString() );

Statics

class Address { constructor( name ) { this.name = name; } static defaultType() { return 'billing'; } } alert( Address.defaultType() );

Getters/Setters

class Person { get fullName() { return `First:${this.first} Last:${this.last}`; } set fullName( value ) { let parts = value.split(' '); this.first = parts[0]; this.last = parts[1]; } } let p = new Person(); p.fullName = 'Kevin Kazmierczak'; alert( p.fullName );

Inheritence

class View { render() { return 'This is the base view'; } } class CollectionView extends View { constructor( collection ) { super( collection ); this.collection = collection; } render() { var content = super.render(); return content += `\nHas ${this.collection.length} records`; } } alert(new CollectionView( [ 1,2 ] ).render());

Modules - Named Exports

// mymodule.js var double = function( x ) { return x + x; } var square = function( x ) { return x * x; } export { double, square } // In another file import { double, square } from 'mymodule'; double( 2 ); square( 3 );

Modules - Default Export

// mymodule.js export default function ( x ) { return x * x; } // In another file import square from 'mymodule' square( 3 );

Modules - Import Namespace

// mymodule.js var name = 'Module'; var location = 'Internets'; export name; export location; // In another file import * as mystuff from 'mymodule' // mystuff.name would be 'Module'

Dynamic Module Loading

Check out ES6 Module Loader Polyfill and SystemJS

System.import( 'mymodule' ).then( function( m ) { alert( 'square: ' + m.square( 7 ) ); });

Module Production Workflows

Use SystemJS to create bundles

Transpiling

  • Babel ( formerly 6to5 )
  • Google Traceur
  • Not everything can be transpiled (polyfills/shims/etc)
  • Sourcemaps let you debug in ES6

Transpiling - Integration

  • Works with all the major build tools
  • Built in WebStorm Watcher (uses Traceur)
  • Webpack ES6 Loader

Transpiling - Notes

  • Babel claims better JSX/React support
  • Babel seems to generate friendly ES5 code
  • Both can export modules to Common.js or AMD
  • Try it out using the online REPL tools
  • Babel REPL
  • Traceur REPL

Compatibility Matrix

ECMAScript support by feature for compilers/polyfills, desktop browsers, servers and mobile.

http://kangax.github.io/compat-table/es6/

Thank You

Questions?