Alejandro Oviedo García
Alejandro is a developer who loves learning new things. He is passionate about education, electronics, Open Source, and community-driven events.
Basically any new syntax (i.e. let, const, etc). Same goes with Proxies and Modules.
var obj = {
// __proto__
__proto__: theProtoObj,
// Shorthand for ‘handler: handler’
handler,
// Methods
toString() {
// Super calls
return "d " + super.toString();
},
// Computed (dynamic) property names
[ 'prop_' + (() => 42)() ]: 42
};
var [d, m, a] = [21, 10, 2015];
//instead of var d = 21, m = 10, a = 2015;
x = 3; y = 4; [x, y] = [y, x]
// instead of temp = [y, x]; x = temp[0]; y = temp[1];
function now() { return [15, 5, 2014, 20, 0]; }
var [d, m] = now(); // m = 15, d = 5
var [,,year] = now(); // no need to accept all elements
function today() { return { d: 15, m: 5, y: 2014 }; }
var { m: month, y: year } = today(); // month = 5, year = 2014
var document = { timestamp: 1400077764803, latitude: -8.137003, longitude: -34.901167, prop1 ....prop999 }
function processDocument({ latitude: lat, longitude: long }) {
// do something here
}
Example:
for(var i = 0; i < 10; i++){
setTimeout(function(){
console.log(i);
}, 10);
}
for(var i = 0; i < 10; i++){
let n = i; setTimeout(function(){ console.log(n); }, 10); }
[1,2,3].forEach(function(x) { console.log(x + 1) }); // can be changed to
[1,2,3].forEach(x => console.log(x + 1));
(a, b) => {
//your code
}
var example = {
count: 0,
increment: function(){
setTimeout(function timeout() {
this.count++;
}, 1000);
}
}
example.increment();
var example = {
count: 0,
increment: function(){
var self = this;
setTimeout(function timeout() {
self.count++;
}, 1000);
}
}
example.increment();
var example = {
count: 0,
increment: function() {
setTimeout(() => this.count++, 1000);
}
}
example.increment();
> let map = new Map();
> map.set('foo', 123);
> map.get('foo')
123
> map.has('foo')
true
> map.delete('foo')
true
> map.has('foo')
false
> let set = new Set();
> set.add('red')
> set.has('red')
true
> set.delete('red')
true
> set.has('red')
false
An iterator is an object with a next method that returns { done, value } tuples.
String, Array, TypedArray, Map and Set are all built-in iterables, because the prototype objects of them all have an @@iterator method.
for(let value of ["a", "b", "c"]){
console.log(value);
}
function idMaker(){
var index = 0;
return {
next: function(){
return {value: index++, done: false};
}
}
}
var it = idMaker();
console.log(it.next().value); // '0'
console.log(it.next().value); // '1'
console.log(it.next().value); // '2'
A generator is a specific type of iterator whose next results are determined by the behavior of its corresponding generator function. Generators also have a throw method, and their next method takes a parameter.
function *foo() {
yield 1;
yield 2;
yield 3;
yield 4;
yield 5;
}
var it = foo();
console.log(it.next()); // { value:1, done:false }
console.log(it.next()); // { value:1, done:false }
console.log(it.next()); // { value:1, done:false }
function *foo(x) {
var y = 2 * (yield (x + 1));
var z = yield (y / 3);
return (x + y + z);
}
var it = foo( 5 );
console.log( it.next() ); // { value:6, done:false }
console.log( it.next( 12 ) ); // { value:8, done:false }
console.log( it.next( 13 ) ); // { value:42, done:true }
function get(filename){
return readJSON('left.json').then(function (left){
return readJSON('right.json').then(function (right){
return {left: left, right: right};
});
});
}
var get = Q.async(function *(){
var left = yield readJSON('left.json');
var right = yield readJSON('right.json');
return {left: left, right: right};
});
By Alejandro Oviedo García
Alejandro is a developer who loves learning new things. He is passionate about education, electronics, Open Source, and community-driven events.