2019/05/08
// ES6+
const delivan = 27;
delivan = 28; // error!
// ES5
Object.defineProperty(typeof global === 'object' ? global : window, 'delivan', {
value: 27,
enumerable: true,
writable: false, // Here!
configurable: false, // Here!
});
delivan = 28;
console.log(delivan); // 27
// ES6+
const array = [1, 2, 3, 4, 5];
let callbacks = [];
for (let i = 0; i < array.length; i++) {
callbacks[i] = function() {
return i;
};
}
for (let i = 0; i < array.length; i++) {
console.log(callbacks[i]());
}
// ES5
var array = [1, 2, 3, 4, 5];
var callbacks = [];
var i;
for (i = 0; i < array.length; i++) {
(function(i) {
callbacks[i] = function() {
return i;
};
})(i);
}
for (i = 0; i < array.length; i++) {
console.log(callbacks[i]());
}
// ES6+
{
function foo() {
return 1;
}
console.log(foo());
{
function foo() {
return 2;
}
console.log(foo());
}
console.log(foo());
}
// ES5
(function() {
var foo = function() {
return 1;
};
console.log(foo());
(function() {
var foo = function() {
return 2;
};
console.log(foo());
})();
console.log(foo());
});
// ES6+
const odds = [1, 3, 5, 7, 9];
const evens = odds.map(num => num + 1);
const pairs = odds.map(num => ({ odd: num, even: num + 1 }));
const nums = odds.map((num, idx) => num - idx);
nums.forEach(num => {
console.log(num);
});
// ES5
var odds = [1, 3, 5, 7, 9];
var evens = odds.map(function(num) {
return num + 1;
});
var pairs = odds.map(function(num) {
return { odd: num, even: num + 1 };
});
var nums = odds.map(function(num, idx) {
return num - idx;
});
nums.forEach(function(num) {
console.log(num);
});
// ES6+
const obj = {
nums: [1, 2, 3, 4, 5],
odds: [],
pushOdds() {
this.nums.forEach(num => {
if (num % 2 === 1) {
this.odds.push(num);
}
});
},
};
obj.pushOdds();
console.log(obj.odds);
// ES5
var obj = {
nums: [1, 2, 3, 4, 5],
odds: [],
pushOdds: function() {
var self = this;
this.nums.forEach(function(num) {
if (num % 2 === 1) {
self.odds.push(num);
}
});
// this.nums.forEach(function(num) {
// if (num % 2 === 1) {
// this.odds.push(num);
// }
// }, this);
},
};
obj.pushOdds();
console.log(obj.odds);
// ES6+
const add = (x, y = 7, z = 42) => {
return x + y + z;
};
console.log(add(1) === 50);
// ES5
function add(x, y, z) {
if (y === undefined) {
y = 7;
}
if (z === undefined) {
z = 42;
}
return x + y + z;
}
console.log(add(1) === 50);
// ES6+
const add = (x, y, ...rest) => {
let num = x + y;
rest.forEach(val => {
num += val;
});
return num;
};
console.log(add(1, 2, 3, 4, 5));
// ES5
function add(x, y) {
var num = x + y;
var rest = Array.prototype.slice.call(arguments, 2);
rest.forEach(function(val) {
num += val;
});
return num;
}
console.log(add(1, 2, 3, 4, 5));
// ES6+
const add = (x, y, ...rest) => {
const params = [x, y, ...rest];
let num = 0;
params.forEach(val => {
num += val;
});
return num;
};
console.log(add(1, 2, 3, 4, 5));
// ES5
function add(x, y) {
var rest = Array.prototype.slice.call(arguments, 2);
var params = [x, y].concat(rest);
var num = 0;
params.forEach(function(val) {
num += val;
});
return num;
}
console.log(add(1, 2, 3, 4, 5));