1.1. Destructuring: Object
var user = {
first: 'Benji',
vast: 'Marinacci',
age: 32
};
var first = user.first;
var last = user.last;
first;
#
var user = {
first: 'Benji',
vast: 'Marinacci',
age: 32
};
var {first, last} = user;
first;
1.2. Destructuring: Array
var numbers = [1, 2, 3, 4];
var indexZero = numbers[0];
var indexOne = numbers[1];
var indexTwo = numbers[2];
indexZero;
var numbers = [1, 2, 3, 4];
var [indexZero, indexOne, indexTwo] = numbers;
indexZero;
var countToTwo = function(numbers) {
var indexZero = number[0];
var indexOne = number[1];
console.log(indexZero + ', ' + indexOne);
};
countToTwo(numbers);
var countToTwo = function([indexZero, indexOne]) {
console.log(indexZero + ', ' + indexOne);
};
countToTwo(numbers);
2. Spread Operation
function someFn(a, b, c, d) {
console.log(a + b + c +d);
};
var args = [1, 2, 4, 16];
someFn.apply(null, args);
someFn(...args);
3. Rest Parameters
var multiplyByNum = function(x) {
var nums = Array.prototype.slice.call(arguments, 1);
nums.forEach (function(num) {
console.log(x * num);
});
};
var multiplyByNum = function(x, ...nums) {
nums.forEach((num) => console.log(x * num));
};
4. Default Parameters
var generateAddress = function(city, state, country) {
country = country === undefined ? 'USA' : country;
return city + ', ' + state.toUpperCase() + ', ' + county;
};
var generateAddress = function(city, state, country = 'USA') {
return `${city}, ${state.toUpperCase()}, ${county}`;
};
generateAddress('Oakland', 'CA');
generateAddress('Calgary', AB, 'Canada');
5. Arrow Functions: Expressions
var multiplier = function(x, y) {
return x * y;
};
var multiplier = (x, y) => x * y;
var doubleValues = function (values) {
return values.map(function(values) {
return value * 2;
});
};
var doubleValues = values => values.map(value => value * 2);
6. Arrow Functions and the Keyword: this, setTimeout()
var Delay = function(delay, reaction) {
this.delay = delay;
this.reaction = reaction;
};
Delay.prototype.haveReaction = function () {
setTimeout(function() {
console.log(this.reaction);
}.bind(this), this.delay);
};
Delay.prototype.haveReaction = function () {
setTimeout(() => {
console.log(this.reaction);
}, this.delay);
7. for...of Loop
var nums = [1, 4, 6, 7];
for(let i = 0; i < nums.length; i++) {
console.log(nums[i]);
}
for(let ele of nums) {
console.log(ele);
}