by @chambaz
I am not an expert.
var obj = {
// old way
doSomething: function() {
},
// new way
doSomething() {
},
// old way
something: something,
// new way
something
};var SOMETHING_IMPORTANT = 1;
SOMETHING_IMPORTANT = 2;
SOMETHING_IMPORTANT === 1; // falseObject.defineProperty(window, "SOMETHING_IMPORTANT", {
value: 1,
writable: false,
configurable: false
});const SOMETHING_IMPORTANT = 1;
SOMETHING_IMPORTANT = 2;
SOMETHING_IMPORTANT === 1; // trueconst IMPORTANT_FUNCTION = function() {
return 'Anything can be assigned to constants';
};var i = 1;
if(i) {
var i = 2;
}
i === 1; // falselet i = 1;
if(i) {
let i = 2;
}
i === 1; // truefunction foo() {
return 1;
}
{
function foo() {
return 2;
}
foo() === 2; // true
}
foo() === 1; // falsefunction foo() {
return 1;
}
{
function foo() {
return 2;
}
foo() === 2; // true
}
foo() === 1; // true(function(x, y, z) {
x++;
return x + y + z;
});(x, y, z) => {
x++;
return x + y + z;
};(x, y, z) => x + y + z;function foo() {
var that = this;
setTimeout(function() {
return that.something;
}, 200);
}function foo() {
setTimeout(function() {
return this.something;
}.bind(this), 100);
}function foo() {
setTimeout(() => this.something, 100);
}function foo(x, y) {
if(!x) {
x = 10;
}
if(!y) {
y = 15;
}
return x + y;
}function foo(x = 10, y = 15) {
return x + y;
}function foo(x, ...y) {
return x + y.length;
}
foo('Digital Surgeons',1,2,3,4,5) // Digital Surgeons5function foo(x) {
var y = Array.prototype.slice.call(arguments, 1);
return x + y.length;
}
foo('Digital Surgeons',1,2,3,4,5) // Digital Surgeons5function foo() {
return arguments.length;
}
var params = [4, 5, 6];
foo('Digital Surgeons', ...params); // 4function foo() {
return arguments.length;
}
var params = [4, 5, 6];
foo.apply(undefined, ['Digital Surgeons'].concat(params)); // 4var player = {
name: 'Harry Kane',
team: 'Spurs',
goals: 21
},
message = player.name + ' of ' +
player.team + ' scored ' +
player.goals + ' in 2014/15'; let player = {
name: 'Harry Kane',
team: 'Spurs',
goals: 21
},
message = `${player.name} of ${player.team} scored ${player.goals} in 2014/15`; let player = {
name: 'Harry Kane',
team: 'Spurs',
goals: 21
},
message = `${player.name} of
${player.team} scored
${player.goals} in 2014/15`;
// Harry Kane of
// Spurs scored
// 21 in 2014/15// utils.js
module.exports.add(x, y) {
return x + y;
}
module.exports.subtract(x, y) {
return x - y;
}
// something.js
var utils = require('lib/utils.js');
utils.add(1, utils.subtract(3, 1)) // 3// utils.js
export add(x, y) {
return x + y;
}
export subtract(x, y) {
return x - y;
}
// something.js
import add from 'lib/utils.js';
add(1, 2) // 3
// something-else.js
import * from 'lib/utils.js';
add(1, subtract(3, 1)) // 3function MyClass(name) {
if(!name) {
name = 'Chambaz';
}
this.tmpl = 'Hello ';
this.name = name;
}
MyClass.prototype.printName = function() {
return this.tmpl+this.name;
};
var myClass = new MyClass('Digital Surgeons');
myClass.printName(); // Hello Digital Surgeonsclass MyClass {
constructor(name = 'Chambaz') {
this.tmpl = 'Hello ';
this.name = name;
}
printName() {
return this.tmpl+this.name;
}
}
var myClass = new MyClass('Digital Surgeons');
myClass.printName(); // Hello Digital Surgeons// have to redefine constructor
function MyOtherClass(name) {
if(!name) {
name = 'Chambaz';
}
this.tmpl = 'Hello ';
this.name = name;
}
MyOtherClass.prototype = Object.create(MyClass.prototype);
MyOtherClass.prototype.constructor = MyOtherClass;
MyOtherClass.prototype.printNameNew = function() {
return this.printName()+', how\'d you like dat inheritence doe';
};
var myOtherClass = new MyOtherClass('Digital Surgeons');
myOtherClass.printNameNew(); // Hello Digital Surgeons, how'd you like dat inheritence doeclass MyOtherClass extends MyClass {
printName() {
let printed = super.printName();
return `${printed}, how'd you like dat ES6 doe`;
}
}
var myOtherClass = new MyOtherClass('Digital Surgeons');
myOtherClass.printName(); // Hello Digital Surgeons, how'd you like dat ES6 doefunction MyClass(name) {
if(!name) {
name = 'Chambaz';
}
this.tmpl = 'hello ';
this.name = name;
}
MyClass.doSomethingNow = function() {
return 'Alright, call it soccer then';
};
MyClass.doSomethingNow(); // Alright, call it soccer thenclass MyClass {
constructor(name = 'Chambaz') {
this.tmpl = 'Hello ';
this.name = name;
}
printName() {
return this.tmpl+this.name;
}
static doSomethingNow() {
return 'Alright, call it soccer then';
}
}
myClass.doSomethingNow(); // Alright, call it soccer thenvar myObj = {
a: 10,
get b() {
return this.a + 1;
},
set c(x) {
this.a = this.a + x;
}
};
myObj.b // 11
myObj.c = 10;
myObj.a // 20class MyClass {
constructor(a = 10) {
this.a = a;
}
get a() {
return this.a + 1;
}
set b(x) {
this.b = this.a + x;
}
}
var myClass = new MyClass(1);
myClass.a // 2
myClass.b = 10;
myClass.b // 11I don't know what I'm talking about
let symbol1 = Symbol();
let symbol2 = Symbol('Digital Surgeons');
let symbol3 = Symbol('Digital Surgeons');
typeof symbol1 // symbol
symbol1 === symbol2 // false
symbol2 === symbol3 // false
let mySymbol = Symbol('Description');
let obj = {
// property key is a symbol
[mySymbol] : 'Something',
// defined inline
[Symbol('key')] : 123,
something : 'Digital',
else: 'Surgeons'
};
Object.getOwnPropertyNames(obj); // [something, else]
Object.getOwnPropertySymbols(obj); // [Symbol('Description'), Symbol('key')]let obj = {
data: ['Digital', 'Surgeons'],
[Symbol.iterator]() {
const self = this;
let index = 0;
return {
next() {
if (index < self.data.length) {
return {
value: `We are ${self.data[index++]}`
};
} else {
return { done: true };
}
}
};
}
};for (let x of obj) {
console.log(x);
}
// We are Digital
// We are Surgeonsfunction *foo() {
var x = 1 + (yield "foo");
return x;
}
foo() // NOTHING!function *foo() {
var x = 1 + (yield "foo");
return x;
}
var something = foo();
something.next(); // { value: "foo", done: false }
something.next(10); // { value: 11, done: true }function *foo() {
yield 1;
yield 2;
yield 3;
yield 4;
return 5;
}
for(let x of foo()) {
console.log(x); // 1, 2, 3, 4, 5
}function *foo() {
yield 3;
yield 4;
}
function *bar() {
yield 1;
yield 2;
yield *foo(); // `yield *` delegates iteration control to `foo()`
yield 5;
}
for (var v of bar()) {
console.log(v);
}
// 1 2 3 4 5// handle AJAX request and call next() when complete
function request(url) {
$.get(url, function(response){
it.next(response);
});
}
// async generator
function *main() {
// pause here until the first request completes
var result1 = yield request("http://some.url.1");
var data = JSON.parse(result1);
// pause here until second request completes
var result2 = yield request("http://some.url.2?id=" + data.id);
var resp = JSON.parse( result2 );
console.log( "The value you asked for: " + resp.value );
}
// get it all started
var it = main();
it.next();Math.sign(-8) // -1
Math.sign(3) // 1
Math.trunc(3.1) // 3
Math.cbrt(8) // 2
'Digital Surgeons'.startsWith('Digital') // true
'Digital Surgeons'.endsWith('Surgeons') // true
'Digital Surgeons'.includes('Surge') // true'Mooney '.repeat(999);
/*
Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney
Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney
Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney
Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney
Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney
Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney
Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney
Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney
Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney
Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney
Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney
Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney
Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney
Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney Mooney