JavaScript today
Where we are, where we're going, what's next
Matt Blair
Principal Software Engineer, Search Platform
mblair@opentable.com
http://blog.mattblair.co/forwardjs-2016-prezzo
https://github.com/duereg/forwardjs-2016-prezzo
Where we are - Browsers
- Chrome 52, Opera 3998%
- Safari TP 298%
- FF 4993%
- Edge 1490%
- Safari 953%
Where we are - Transpilers
- Babel 6.4.674%
- TypeScript 1.8.060%
- Traceur 0.0.10258%
- Closure v2016031542%
- JSX 0.9.8920%
Where we are - Runtimes
- XS6 7.1.4597%
- Node 6.093%
-
EchoJS
0.0.0-alpha2
66%
- Node 5.056%
- Node 4.050%
What Works Everywhere?
Syntax
- * Some environments require a 'use strict' statement
- let
- const
- Arrow Functions
- Object Literal Extensions
- String Templates
let
function varTest() {
var x = 31;
if (true) {
var x = 71; // same variable!
console.log(x); // 71
}
console.log(x); // 71
}
let
function letTest() {
let x = 31;
if (true) {
let x = 71; // different variable
console.log(x); // 71
}
console.log(x); // 31
}
let errors
if (x) {
let foo;
let foo; // TypeError thrown.
}
function do_something() {
console.log(foo); // ReferenceError
let foo = 2;
}
const
const MY_FAV = 7;
MY_FAV = 20; //fails FF, Chrome, not Safari
console.log("my favorite number is: " + MY_FAV); // 7
const MY_FAV = 20; //throws
var MY_FAV = 20; //throws
console.log("my favorite number is " + MY_FAV); // 7
const
const FOO; // SyntaxError: missing = in const declaration
const MY_OBJECT = {"key": "value"}; // works
//fails FF, Chrome, not Safari
MY_OBJECT = {"OTHER_KEY": "value"};
// Object attributes are not protected
MY_OBJECT.key = "otherValue";
Arrow Functions
// Arrow functions are all bound to this
(param) => { param++; } // function(param) { param++; }
// implicit return
(a1, a2, aN) => a1 + a2 // function(a1, a2, aN) { return a1 + a2; }
// Parentheses are optional when there's one parameter
(singleParam) => { statements }
singleParam => { statements }
Object Literal Extensions
var a = 103, bar = 'foo'
var obj = {
a: a,
m: function(z) {return z + this.a},
}
obj[bar] = 104 // obj.foo
Object.defineProperty(obj, 'getA', {
get: function() {return this.a()},
})
Object Literal Extensions
var a = 103, bar = 'foo'
var obj = {
a, // a:a, object shorthand
m(z) {return z + this.a}, // function shorthand
[bar]: 104, // Computed Property Names
get getA() {return this.a}
}
String Templating
console.log("string text line 1\n string text line 2");
// is equal to
console.log(`string text line 1
string text line 2`);
var a = 5, b = 10;
console.log(`Fifteen is ${a + b} and\nnot ${2 * a + b}.`);
What Works Everywhere?
Data Structures
- Set
- Map
- WeakSet
- WeakMap
- Symbol
- Promise
Set
let s = new Set()
s.add("hello").add("hello").add({action: 'goodbye'})
s.size === 2
s.has("hello") === true
for (let key of s.values()) // insertion order
console.log(key)
Map
let s = { foo: 'bar' }
let m = new Map()
m.set("hello", 42)
m.set(s, 34)
m.get(s) === 34
m.size === 2
for (let [ key, val ] of m.entries())
console.log(key + " = " + val)
WeakSet
let thingToDelete = {};
let ws = new WeakSet()
ws.set(thingToDelete) //cannot be primitive
ws.has(thingToDelete) === true
thingToDelete = null;
ws.has(thingToDelete) === false
WeakMap
let thingToDelete = {};
let wm = new WeakMap()
wm.set(thingToDelete, 42) //cannot be primitive
wm.get(thingToDelete) === 42
thingToDelete = null;
wm.has(thingToDelete) === false
wm.get(thingToDelete) === undefined
Symbol
Symbol("foo") !== Symbol("foo") // every Symbol is unique
const foo = Symbol()
const bar = Symbol()
typeof foo === "symbol"
typeof bar === "symbol"
let obj = {}
obj[foo] = "foo"
obj[bar] = "bar"
JSON.stringify(obj) // {}
Object.keys(obj) // []
What are we missing?
- Proxies
- Destructuring
- Subclassing Array, Function, etc
What are my options?
- Client SPA in Chrome 52, Opera 39
- 98% of ES6
- Proxies, Destructuring, Subclassing
- no module import/export
- IofT Application using XS6 Runtime
- 98% of ES6
- limited to Kinoma hardware
- Node 4/5 App, Transpiled with Babel
- 75% of ES6
- Destructuring, Module Import/Export
- ESNext, JSX, TypeScript, Flow, etc
- Node 6 App
- 93% of ES6
- Destructuring, Limited Proxies, Subclassing
Thanks!
For more information:
- http://kangax.github.io/compat-table/es6/
- http://es6-features.org/
- http://blog.mattblair.co/forwardjs-2016-prezzo
- https://github.com/duereg/forwardjs-2016-prezzo