Information from ECMA6 Compat Table
Information from ECMA6 Compat Table
Information from ECMA6 Compat Table
Information from CanIUse
Information from dnbard/es6-guide
var odds = evens.map(v => v + 1);
var nums = evens.map((v, i) => v + i);
var odds = evens.map(function(v){
return v + 1;
});
var nums = evens.map(function(v, i){
return v + i;
});
ECMAScript 5
ECMAScript 6
var events = require('./events');
function Entity(name){
this.name = name;
events.addListener('name.changed', (newName) => {
this.name = newName;
});
}
var myEntity = new Entity('Some Name');
events.trigger('name.changed', 'Another Name')
var events = require('./events');
function Entity(name){
this.name = name;
events.addListener('name.changed',
function(newName){
this.name = newName;
}.bind(this));
}
var myEntity = new Entity('Some Name');
events.trigger('name.changed', 'Another Name')
class MyComponent {
myMethod(){
return true;
}
}
class Text extends MyComponent {
constructor(){
super();
}
render() {
var el = $('<span>1</span>');
$('id').appendChild(el);
}
}
function MyComponent(){
}
MyComponent.prototype.myMethod = function(){
return true;
}
function Text(){
MyComponent.call(this);
}
Text.prototype = MyComponent.prototype;
Text.prototype.render = function(){
var el = $('<span>1</span>');
$('id').appendChild(el);
}
ECMAScript 5
ECMAScript 6
var handler = () => {};
var obj = {
handler,
toString() {
return "d ";
},
[ "prop_" + (() => 42)() ]: 42
};
var handler = function(){};
var obj = {
handler: handler,
toString: function() {
return "d ";
}
};
obj[(function(){
return 'prop_' + 42;
})()] = 42;
ECMAScript 5
ECMAScript 6
var user = {
name: 'Alex'
};
`${new Date()}: ${user.name} has logged in.`;
var user = {
name: 'Alex'
};
new Date() + ': ' + user.name +
' has logged in.';
ECMAScript 5
ECMAScript 6
var [a, ,b] = [1,2,3];
a === 1;
b === 3;
ECMAScript 6
function g({name: x}) {
console.log(x);
}
g({name: 5}); //5
ECMAScript 6
function g(a, b = 10) {
console.log(a + b);
}
g(5); //15
ECMAScript 6
function g(a, ...b) {
console.log(a + b.length);
}
g(5, "Hello", "World"); //7
ECMAScript 6
function g(a, b) {
console.log(a + b);
}
g(...[5, 2]); //7
ECMAScript 6
const message = "Hello, World";
message = "Hi"; //error
ECMAScript 6
function letTest() {
let x = 31;
if (true) {
let x = 71; // different variable
console.log(x); // 71
}
console.log(x); // 31
}
ECMAScript 6
function* idMaker(){
var index = 0;
while(true){
yield index++;
}
}
var gen = idMaker();
console.log(gen.next().value); // 0
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
ECMAScript 6
spawn(function*() {
var data = yield $.ajax(url);
var status = $('#status').html('Download complete.');
yield status.fadeIn().promise();
yield sleep(2000);
status.fadeOut();
});
Used Mozilla's Task.js lib
// lib/math.js
export function sum(x, y) {
return x + y;
}
export var pi = 3.141593;
// app.js
import * as math from "lib/math";
alert("2π = " + math.sum(math.pi, math.pi));
// otherApp.js
import {sum, pi} from "lib/math";
alert("2π = " + sum(pi, pi));
// lib/mathplusplus.js
export * from "lib/math";
export var e = 2.71828182846;
export default function(x) {
return Math.exp(x);
}
// app.js
import exp, {pi, e} from "lib/mathplusplus";
alert("2π = " + exp(pi, e));
var m = new Map();
m.set("hello", 42);
m.set(s, 34);
m.get(s) == 34;
m.get("Hello") == 42;
var s = new Set();
s.add("hello")
.add("goodbye")
.add("hello");
s.size === 2;
s.has("hello") === true;
let wm = new WeakMap();
let obj = document.querySelector('#something');
wm.set(obj, { extra: 42 });
wm.get(obj); //42
var ws = new WeakSet();
ws.add({ data: 42 });
var ws = new WeakSet();
ws.add({ data: 42 });
"abcde".includes("cd") // true
"abc".repeat(3) // "abcabcabc"
Array.from(document.querySelectorAll("*")) // Returns a real Array
[0, 0, 0].fill(7, 1) // [0,7,7]
[1,2,3].findIndex(x => x == 2) // 1
["a", "b", "c"].entries() // iterator [0, "a"], [1,"b"], [2,"c"]
["a", "b", "c"].keys() // iterator 0, 1, 2
["a", "b", "c"].values() // iterator "a", "b", "c"
var a = { a: 1 };
Object.assign(a, { b: 2 }); // Object {a: 1, b: 2}
0b111110111 === 503 // true
0o767 === 503 // true
function doAjax(url){
return new Promise(function(resolve, reject){
$.ajax({
url: url,
success: resolve,
error: reject
});
});
}
doAjax('http://example.com').then((data) => {
console.log(data)
}).catch((err){
console.error(err);
});