KEMBAR78
Living in harmony - a brief into to ES6 | PPTX
Living in harmony
A brief intro to ES6
Maryland.JS Meetup
08/27/2014
Who am I?
JONESY ROCKIN’
THE HIGH SOCKS!
ECMA-what?
A (very) brief history…
JAVASCRIPT CREATED
ORIGINALLY NAMED MOCHA, THEN LIVESCRIPT
NETSCAPE NAVIGATOR 2.0
SUPPORT FOR JAVASCRIPT
1995
1996
SPEC WORK BEGINS
ECMA-262 & ECMASCRIPT BORN
1ST EDITION1997
2ND EDITION1998
3RD EDITION
THE FOUNDATION OF MODERN JAVASCRIPT
1999
5TH EDITION
BACKWARDS-COMPAT, STRICT MODE, JSON
2009
6TH EDITION (HARMONY)
FEATURE FREEZE 08/2014, PUBLISH 06/2015
NOW
7TH EDITION
VERY EARLY STAGES, DISCUSSIONS
…
1996
Features
So. Many. Features.
Block-level scoping
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let
function demoLet() {
{
var a = 2;
let b = 2;
}
console.log(a); // 2
console.log(b); // ReferenceError
}
for…of
let arr = ["one", "two", "three"];
for(let i in arr) {
// logs keys: 0, 1, 2
console.log(i);
}
for(let i of arr) {
// logs values: ”one", "two", "three"
console.log(i);
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
Arrow function
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/arrow_functions
function demoArrowFunction2() {
var squared = x => x * x;
console.log(squared(7)); // 49
}
Arrow function
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/arrow_functions
function demoArrowFunction1() {
function Item() {
this.y = 2;
setTimeout(function() {
console.log(this.y); // undefined
}, 500);
setTimeout(() => {
console.log(this.y); // 2
}, 1000);
}
var item = new Item();
}
Default parameters
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/default_parameters
function demoDefaultParams() {
function personalInfo(age, firstName = "John") {
// ooh string templates too!
return `${firstName} ${lastName} is ${age}`;
}
console.log(personalInfo(34, "Rich")); // Rich is 34
console.log(personalInfo(100)); // John is 100
}
Spread operator
function demoSpread() {
// array literals
var fruits = ["apples", "oranges"];
var shoppingList = ["bananas", ...fruits];
console.log(shoppingList); // ["bananas", "apples", "oranges"]
// function arguments
function trySpread(one, two) {
console.log(one, two); // ["apples", "oranges"]
}
trySpread(...fruits);
}
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator
Destructuring
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
function demoDestructure() {
// object destructuring
let someObj = {
x: 20,
y: 30
};
let {x, y} = someObj;
console.log(x); // 20
console.log(y); // 30
}
Destructuring assignment
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
function demoDestructure() {
// array desctructuring
function f() {
return [1, 2, 3];
}
let [first,,third] = f(); // ignore 2nd element
console.log(first); // 1
console.log(third); // 3
}
Comprehensions
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Array_comprehensions
function demoComprehensions() {
var letters = ["A", "B", "C"];
var numbers = [1, 2, 3];
// similar to letters.map
var lowerCased = [for (letter of letters) letter.toLowerCase()];
console.log(lowerCased); // ["a", "b", "c"]
// similar to letters.filter
var filtered = [for (letter of letters) if (letter !== "A") letter];
console.log(filtered); // ["B", "C"]
// multiple arrays
var combined = [for (l of letters) for (n of numbers) l + n];
console.log(combined); // ["A1", "A2", "A3", "B1", ...]
}
Template strings
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings
function demoTemplateStrings() {
var x = 1;
var y = 1;
console.log(`x + y = ${x + y}`); // x + y = 2
}
Collections: Set
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
function demoSet() {
var arrayWithDupes = [1, 1, 2, 3, 3];
var deDuped = new Set(arrayWithDupes);
console.log(deDuped); // [1, 2, 3]
console.log(deDuped.has(8)); // false
}
Collections: Map
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
function demoMap() {
var myMap = new Map();
var someObj = {};
myMap.set(50, "int");
myMap.set("test", "string");
myMap.set(someObj, "{}");
console.log(myMap.get(50)); // "int"
console.log(myMap.get("test")); // "string"
console.log(myMap.get(someObj)); // "{}"
}
Generators
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*
function demoGenerators() {
function* fibonacci() {
var fn1 = 1, fn2 = 1;
while(1) {
var current = fn2;
fn2 = fn1;
fn1 = fn1 + current;
yield current;
}
}
var sequence = fibonacci();
console.log(sequence.next().value); // 1
console.log(sequence.next().value); // 1
console.log(sequence.next().value); // 2
console.log(sequence.next().value); // 3
}
Classes
class Vehicle {
constructor(name) {
this.name = name;
this.hasWings = false;
}
canFly() {
return this.hasWings;
}
}
}
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-class-definitions
Classes
class Car extends Vehicle {
constructor(name, make, model) {
super(name);
this.hasWings = false;
this.make = make;
this.model = model;
}
}
var myCar = new Car("A-Team Van", "GMC", "Vandura");
console.log(myCar.canFly()); // false
console.log(myCar.make); // "GMC"
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-class-definitions
Classes
class Plane extends Vehicle {
constructor(name) {
super(name);
this.hasWings = true;
}
}
var myPlane = new Plane("The Wright Flyer");
console.log(myPlane.canFly()); // true
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-class-definitions
Modules
// lib/math.js
export function sum(x, y) {
return x + y;
}
// app.js (using module)
module math from "lib/math";
console.log(math.sum(2, 3)); // 5
// app.js (using import)
import sum from "lib/math";
console.log(sum(2, 3)); // 5
http://people.mozilla.org/~jorendorff/es6-draft.html#sec-modules
…and all this jazz
• Promises
• New Array functions
• New Math functions
• New Number functions
• New Object functions
• WeakMap
• WeakSet
• Binary and octal literals
• Proxy
• Symbol
• Full Unicode
• …
Resources
Go on, get!
Light reading
• ECMAScript 6 Draft Spec
• ECMAScript Discussion Archives
• ECMAScript 6 Resources For The Curious JavaScripter
• List of ES6 features
• ECMAScript 6 Support in Mozilla
For your viewing pleasure
• egghead.io ES6 videos
• ECMAScript 6 on YouTube
ECMAScript 6 compatibility table
Traceur compiler
grunt-traceur-latest
http://www.es6fiddle.net/
This talk and code
https://github.com/richleland/es6-talk
Follow me
richleland on github/twitter
THANK YOU!

Living in harmony - a brief into to ES6

Editor's Notes

  • #3 whirlwind tour of ES6 - history, some of the features, resources to get you going before I jump into things…
  • #5 Based on my gravatar, I’m a super mysterious dude (who wears fingerless gloves)!
  • #6 Except I’m not. I’m a husband, father of super-awesome twins
  • #7 Huge Orioles fan
  • #8 I work here at Message Systems where we use AngularJS and Node.js daily official role: lead software eng
  • #9 Unofficial role: defeat these guys at ping pong Enough about me…
  • #10 quick rundown of the history of ECMA script - past, present, future
  • #11 European Computer Manufacturers Association —> ecma international 4th edition started in 2000, abandoned in 2003 Sources: http://en.wikipedia.org/wiki/Ecma_International, http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/, https://twitter.com/awbjs/status/474662357516689410, https://www.w3.org/community/webed/wiki/A_Short_History_of_JavaScript
  • #12 Let’s dive in to the features
  • #13 scoping in JS is.. interesting
  • #14 iterate over values can also be achieved with forEach
  • #15 no need for boilerplate anon functions using parameters
  • #16 no more self = this (or that = this) no params or params, up to you
  • #17 coming from other languages like Python - not having this sucks!
  • #18 expansion of multiple arguments
  • #19 xxx
  • #20 xxx
  • #21 xxx
  • #22 xxx
  • #23 xxx
  • #24 xxx
  • #25 xxx
  • #26 xxx
  • #27 xxx
  • #28 xxx
  • #29 xxx
  • #30 bullet points - other items
  • #31 xxx
  • #32 tons of resources - search google for es6 resources
  • #37 compilers that convert ES6 to ES5
  • #38 xxx
  • #39 xxx