What is ES6?
It’s ECMAScript 6
, the standard of the next generation of javascript. Now ECMAScript 6
is renamed to ECMAScript 2015, that mean it was released in 2015.
ECMAScript
is a specification, Javascript
is an implementation.
ECMA is an international, they standardize the scripting language.
There are released versions of ECMAScript
.
Other little languages compile into javascript, such as CoffeeScript, TypeScript. ## New features
- Constants
1const PI = 3.14 2PI > 3.0
1 2 3 4 5 6 7 8 9 |
|
- Arrow Functions
1odds = evens.map(v => v + 1) 2pairs = evens.map(v => ({ even: v, odd: v + 1 })) 3nums = evens.map((v, i) => v + i)
1 2 3 |
|
- Default Parameter Values
1function f (x, y = 7, z = 42) { 2 return x + y + z 3} 4f(1) === 50
1 2 3 4 5 6 7 8 |
|
- Enhanced Object Properties
1// Property Shorthand 2obj = { x, y } 3 4// Computed Property Names 5obj = { 6 foo: "bar", 7 [ "prop_" + foo() ]: 42 8} 9 10// Method Properties 11obj = { 12 foo (a, b) { 13 … 14 }, 15 bar (x, y) { 16 … 17 }, 18 *quux (x, y) { 19 … 20 } 21}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
- Modules
1// lib/math.js 2export function sum (x, y) { return x + y } 3export var pi = 3.141593 4 5// someApp.js 6import * as math from "lib/math" 7console.log("2π = " + math.sum(math.pi, math.pi)) 8 9// otherApp.js 10import { sum, pi } from "lib/math" 11console.log("2π = " + sum(pi, pi))
1 2 3 4 5 6 7 8 9 10 11 12 |
|
- Classes
1class Shape { 2 constructor (id, x, y) { 3 this.id = id 4 this.move(x, y) 5 } 6 move (x, y) { 7 this.x = x 8 this.y = y 9 } 10}
1 2 3 4 5 6 7 8 |
|
- Generator Functions
1function* idMaker(){ 2 var index = 0; 3 while(index < 3) 4 yield index++; 5} 6 7var gen = idMaker(); 8 9console.log(gen.next().value); // 0 10console.log(gen.next().value); // 1 11console.log(gen.next().value); // 2 12console.log(gen.next().value); // undefined
- Spread operator
1function add(a, b) { 2 return a + b; 3} 4 5let nums = [5, 4]; 6 7console.log(add(...nums)); 8 9let foo = "foo" 10let chars = [ ...str ] // [ "f", "o", "o" ]
- Proxying
1let target = { 2 foo: "Welcome, foo" 3} 4let proxy = new Proxy(target, { 5 get (receiver, name) { 6 return name in receiver ? receiver[name] : `Hello, ${name}` 7 } 8}) 9proxy.foo === "Welcome, foo" 10proxy.world === "Hello, world"
- Map and Set Data-Structure
To see more, we could visit, ECMAScript 6 — New Features: Overview & Comparison
Compile
Compile ES6 to ES5.
Why need compile
? because the browser support of ES6 isn’t enough.
Babel
is a new name of ES6to5
. It can integrate with many js / node frameworks.
Conclusion
OK, let’s start ES6 now!!!