88. Block Scoping Functions
var a = 5;
var b = 10;
if (a === 5) {
let a = 4;
var b = 1;
console.log(a); // 4
console.log(b); // 1
}
console.log(a); // 5
console.log(b); // 1
var a = 5;
var b = 10;
if (a === 5) {
(function () {
var a = 4;
b = 1;
console.log(a); // 4
console.log(b); // 1
})();
}
console.log(a); // 5
console.log(b); // 1
88
89. Template
var user = { name: 'Caitlin Potter' };
console.log('Thanks for V8, ' + user.name + '.');
var user = {name: 'Caitlin Potter'};
console.log(`Thanks for V8, ${user.name}.`);
89
90. Computed Property Names
var prefix = 'foo';
var test= {
[prefix + 'bar']: 'hello',
[prefix + 'baz']: 'world'
};
console.log(test['foobar']);
// -> hello
console.log(test['foobaz']);
// -> world
var prefix = 'foo';
var test= {};
test[prefix + 'bar'] = 'hello';
test[prefix + 'baz'] = 'world';
console.log(test['foobar']);
// -> hello
console.log(test['foobaz']);
// -> world
90
91. Destructuring Assignment
function f(x, y) {
if (y === undefined) {
y = 12;
}
return x + y;
}
f(3) === 15;
function f(x, y = 12) {
return x + y;
}
f(3) === 15;
91
93. Module
var math = require('lib/math');
console.log('2π = ' + math.sum(math.pi, math.pi));
import math from 'lib/math';
console.log('2π = ' + math.sum(math.pi, math.pi));
93
94. Property Method Assignment
var object = {
value: 42,
toString: function toString() {
return this.value;
}
};
var test= {
value: 42,
toString() {
return this.value;
}
};
94
95. Rest Parameters
function f(x) {
var y = [];
y.push.apply(y, arguments) &&
y.shift();
// y is an Array
return x * y.length;
}
function f(x, ...y) {
// y is an Array
return x * y.length;
}
console.log(f(3, 'hello', true) === 6);
95
96. Spread Operator
function f(x, y, z) {
return x + y + z;
}
f.apply(null, [1, 2, 3]) === 6;
function f(x, y, z) {
return x + y + z;
}
f(...[1,2,3]) === 6;
96