Here are the slides that I gave for The Arizona Software Community meetup.
http://www.meetup.com/azsoftcom/events/222936544/
This was a gentle introduction to some of the features in EcmaScript 2015 and how and why you may use them.
15. ES6 Classes
⢠Simple sugar over the the prototype-based
OO pattern
⢠A more convenient form makes classes
easier to use
⢠Classes support inheritance, super calls,
instance and static methods and
constructors
16. class Project {â¨
constructor(name) {â¨
this.name = name;â¨
}â¨
â¨
start() {â¨
return `Project ${this.name} starting`;â¨
}â¨
}â¨
â¨
var project = new Project("Website");â¨
project.start(); // "Project Website starting"
18. ES6 INheritance
⢠Inheritance is possible via the prototype
⢠You can inherit from a base class using the
extends keyword
⢠You must call super before you can access
this in a subclass
21. ES6 MODULES
⢠Language-level support for modules for
component deďŹnition
⢠Best of both both CommonJS and AMD
⢠Named exports and default exports
30. function printName() {â¨
var name; // variable declaration is hoisted to the topâ¨
if(true) {â¨
name = "Rafael";â¨
}â¨
console.log(name); // Rafaelâ¨
}
32. function printName() {â¨
var name = "Hey";â¨
if(true) {â¨
let name = "Rafael";â¨
console.log(name); // Rafaelâ¨
}â¨
console.log(name); // Heyâ¨
}
33.
34. if (true) { // enter new scope, TDZ startsâ¨
â¨
// Uninitialized binding for `tmp` is createdâ¨
tmp = 'abc'; // ReferenceErrorâ¨
console.log(tmp); // ReferenceErrorâ¨
â¨
let tmp; // TDZ ends, `tmp` is initialized with `undefined`â¨
console.log(tmp); // undefinedâ¨
â¨
tmp = 123;â¨
console.log(tmp); // 123â¨
}
37. ES6 Arrow Functions
⢠Arrow function expression aka fat arrow
functions are a shorter syntax
⢠Lexically binds the this value
⢠Arrow functions are always anonymous
38. var numbers = [1,2,3,4,5];â¨
var timesTwo = numbers.map(function (number) {â¨
return number * 2;â¨
});â¨
console.log(timesTwo); // [2, 4, 6, 8, 10]
39. var numbers = [1,2,3,4,5];â¨
var timesTwo = numbers.map((number) => number * 2);â¨
console.log(timesTwo); // [2, 4, 6, 8, 10]
40. var numbers = [1,2,3,4,5];â¨
var timesTwo = numbers.map(number => number * 2);â¨
console.log(timesTwo); // [2, 4, 6, 8, 10]
53. ES6 Object Literals
⢠Object literals are extended to be more
convenient and more like deďŹning a class
⢠Better property and method declarations
54. var x = 10;â¨
var y = 30;â¨
â¨
var coordinates = {â¨
x: x,â¨
y: yâ¨
};â¨
â¨
console.log(coordinates); // { x: 10, y: 30 }
55. let x = 10;â¨
let y = 30;â¨
â¨
let coordinates = { x, y };â¨
â¨
console.log(coordinates); // { x: 10, y: 30 }
56. let x = 10;â¨
let y = 30;â¨
â¨
let coordinates = {â¨
x,â¨
y,â¨
z: 10,â¨
};â¨
â¨
console.log(coordinates); // { x: 10, y: 30, z: 10 }
60. ES6 Array API
⢠Array.from converts array-like objects into arrays
⢠Array.keys, Array.values and Array.entries are
handy for iterating over arrays
⢠Array.ďŹnd returns the ďŹrst element that the
callback returns true
â˘Array.ďŹndIndex returns the index of the ďŹrst
element that the callback returns true
61. // Array-like object (arguments) to Arrayâ¨
function f() {â¨
return Array.from(arguments);â¨
}â¨
â¨
f(1, 2, 3);â¨
// [1, 2, 3]â¨
â¨
â¨
// Any iterable object...â¨
// Setâ¨
var s = new Set(["foo", window]);â¨
Array.from(s);â¨
// ["foo", window]â¨
â¨
â¨
// Mapâ¨
var m = new Map([[1, 2], [2, 4], [4, 8]]);â¨
Array.from(m);â¨
// [[1, 2], [2, 4], [4, 8]]â¨
â¨
â¨
// Stringâ¨
Array.from("foo");â¨
// ["f", "o", "o"]â¨
68. ES6 SET
⢠Lets you store unique values of any type
⢠You can store primitive values or object
references
69. var mySet = new Set();â¨
â¨
mySet.add(1);â¨
mySet.add(5);â¨
mySet.add("some text");â¨
â¨
mySet.has(1); // trueâ¨
mySet.has(3); // false, 3 has not been added to the setâ¨
mySet.has(5); // trueâ¨
mySet.has(Math.sqrt(25)); // trueâ¨
mySet.has("Some Text".toLowerCase()); // trueâ¨
â¨
mySet.size; // 3â¨
â¨
mySet.delete(5); // removes 5 from the setâ¨
mySet.has(5); // false, 5 has been removedâ¨
â¨
mySet.size; // 2, we just removed one value
71. ES6 MAP
⢠Simple key/value map
⢠Any value (object or primitive value) can be
used as either a key or a value
72. var myMap = new Map();â¨
â¨
var keyObj = {},â¨
keyFunc = function () {},â¨
keyString = "a string";â¨
â¨
// setting the valuesâ¨
myMap.set(keyString, "value associated with 'a string'");â¨
myMap.set(keyObj, "value associated with keyObj");â¨
myMap.set(keyFunc, "value associated with keyFunc");â¨
â¨
myMap.size; // 3â¨
â¨
// getting the valuesâ¨
myMap.get(keyString); // "value associated with 'a string'"â¨
myMap.get(keyObj); // "value associated with keyObj"â¨
myMap.get(keyFunc); // "value associated with keyFunc"â¨
â¨
myMap.get("a string"); // "value associated with 'a string'"â¨
// because keyString === 'a string'â¨
myMap.get({}); // undefined, because keyObj !== {}â¨
myMap.get(function() {}) // undefined, because keyFunc !==
function () {}