Last active
September 11, 2015 07:08
-
-
Save edgarmarkosov/769296746f809c70d2b4 to your computer and use it in GitHub Desktop.
Node JS v. 4.0.0, ES6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//BE CAREFUL WITH BROWSER COMPATIBILITY | |
1.Template Strings | |
var message = ` | |
kjfgskfgsg | |
sdflkgdsflgldfsg | |
mdsfg.dfgdfs;gjklsdfgjksdflg | |
`; | |
console.log(message) | |
2. String interpolation | |
var name = 'Schroedinger'; | |
// stop doing this ... | |
var message = 'Hello ' + name + ', how is your cat?'; | |
var message = ['Hello ', name, ', how is your cat?'].join(''); | |
var message = require('util').format('Hello %s, how is your cat?', name); | |
// and instead do that ... | |
var message = `Hello ${name}, how is your cat?`; | |
3. Tagged template strings. | |
With them you are able to modify the output of template strings using a function. | |
The first argument contains an array of string literals ("Hello " and " world" in this example). | |
The second, and each argument after the first one, are the values of the processed (or sometimes called cooked) substitution expressions ("15" and "50" here). | |
var a = 5; | |
var b = 10; | |
function tag(strings, ...values) { | |
console.log(strings[0]); // "Hello " | |
console.log(strings[1]); // " world " | |
console.log(values[0]); // 15 | |
console.log(values[1]); // 50 | |
return "Bazinga!"; | |
} | |
tag`Hello ${ a + b } world ${ a * b}`; | |
// "Bazinga!" | |
4. Raw strings | |
function tag(strings, ...values) { | |
console.log(strings.raw[0]); | |
// "string text line 1 \\n string text line 2" | |
} | |
tag`string text line 1 \n string text line 2`; | |
5. Classes | |
class Pet { | |
constructor(name) { | |
this._name = name; | |
} | |
sayHello() { | |
console.log('*scratch*'); | |
} | |
get name() { | |
return this._name; | |
} | |
} | |
class Cat extends Pet { | |
constructor(name) { | |
super(name); | |
} | |
sayHello() { | |
super.sayHello(); | |
console.log('miaaaauw'); | |
} | |
} | |
6. Arrow Functions | |
The dynamic binding of this on function invocation always causes some confusion, and people worked around it in a couple of ways: | |
Cat.prototype.notifyListeners = function () { | |
var self = this; | |
this._listeners.forEach(function (listener) { | |
self.notifyListener(listener); | |
}); | |
}; | |
Cat.prototype.notifyListeners = function () { | |
this._listeners.forEach(function (listener) { | |
this.notifyListener(listener); | |
}.bind(this)); | |
}; | |
Now you can just use fat arrow functions: | |
Cat.prototype.notifyListeners = function () { | |
this._listeners.forEach((listener) => { | |
this.notifyListener(listener); | |
}); | |
}; | |
7. Object Literals | |
var age = 10, name = 'Petsy', size = 32; | |
// instead of this ... | |
var cat = { | |
age: age, | |
name: name, | |
size: size | |
}; | |
// ... do this ... | |
var cat = { | |
age, | |
name, | |
size | |
}; | |
8. Promises | |
var p1 = new Promise(function (resolve, reject) {}); | |
var p2 = Promise.resolve(20); | |
var p3 = Promise.reject(new Error()); | |
var p4 = Promise.all(p1, p2); | |
var p5 = Promise.race(p1, p2); | |
p1.then(() => {}).catch(() => {}); | |
9. let and const | |
var x = 20; | |
(function () { | |
if (x === 20) { | |
var x = 30; | |
} | |
return x; | |
}()); // -> undefined | |
let x = 20; | |
(function () { | |
if (x === 20) { | |
let x = 30; | |
} | |
return x; | |
}()); // -> 20 | |
var MY_CONST = 42; // no, no | |
const MY_CONST = 42; // yes, yes | |
MY_CONST = 10 // with const, this is no longer possible |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment