let, const, () => {}
Airbnb JavaScript Style Guide()
@humanamburu
if (42 === 42) {
let value = 42;
}
console.log(value); // throws error
var glob = 42;
window.glob === glob
true
let glob = 42;
window.glob === glob
false
var i = 'hello?';
for (var i = 0; i < 10; i++) {
console.log(i);
}
i === 10
true
let i = 'hello?';
for (let i = 0; i < 10; i++) {
console.log(i);
}
i === 'hello?'
true
let value = 1;
let value = 2; // SyntaxError: Identifier 'value' has already been declared
const value = 1; // SyntaxError: Identifier 'value' has already been declared
function func() {
let foo = 5;
if (true) {
let foo = 10; // shadows outer `foo`
console.log(foo); // 10
}
console.log(foo); // 5
}
but may not be accessed in any way until the variable’s declaration is evaluated
function printOne() {
console.log(one); // throws Error
let one = 2; // why not?
}
printOne();
YOU
let x = 'outer scope';
(function() {
console.log(x); // hoist but throw error
let x = 'inner scope';
}());
const variable = func();
const TDZ = 2;
function func() { return TDZ; }
(function(a = b, b) {
console.log(a, b);
}(undefined, 2));
const THE_ANSWER_OF_LIFE_AND_EVERYTHING = 42;
THE_ANSWER_OF_LIFE_AND_EVERYTHING = 2; // Uncaught TypeError:
// Assignment to constant variable.
THE_ANSWER_OF_LIFE_AND_EVERYTHING++; // Nice tray, but No.
const heh; // SyntaxError: Missing initializer in const declaration
const mutable = {
type: 'THE_ANSWER_OF_LIFE_AND_EVERYTHING',
value: 42
};
mutable.value = 1;
// Mutated object
mutable.value;
=> 1
const mutableArray = [];
mutableArray.push(1);
// Mutated array
mutableArray;
=> [1]
() => { reuturn 41 + 1; } // without parameters
() => 41 + 1 // expression without parameters, equal to previous
(a) => { return 41 + a; } // one parameter
a => 41 + a // expression, equal to previous
(a, b) => { return a + b; } // several parameters
(a, b) => a + b // expression, equal to previous
An arrow function expression has a shorter syntax than a function expression and does not bind its own this, arguments, super.
const array = [1, 2, 3, 4, 5];
// callbacks looks good
array.map(item => item * 2);
//equals
array.map(function(item) {
return item * 2;
});
function Guests(array) {
this.guests = array;
}
Guests.prototype.bePolite = function(prefixes) {
return prefixes.map(function(prefix, index) {
return prefix + this.guests[index];
})
}
const guests = new Guests(['John', 'Mary', 'Denis']);
guests.bePolite(['Mr. ', 'Ms. ', 'Mr. ']); // error
function Guests(array) {
this.guests = array;
}
Guests.prototype.bePolite = function(prefixes) {
// fix
return prefixes.map((prefix, index) => {
return prefix + this.guests[index];
});
}
const guests = new Guests(['John', 'Mary', 'Denis']);
guests.bePolite(['Mr. ', 'Ms. ', 'Mr. ']); // ["Mr. John", "Ms. Mary", "Mr. Denis"]
function HiMan(name) {
this.name = name;
}
HiMan.prototype.hi = function() {
setTimeout(function() {
console.info(`Hi, ${this.name}`);
});
}
//fixed version
HiMan.prototype.hi = function() {
setTimeout(() => {
console.info(`Hi, ${this.name}`);
});
}
var obj = {
i: 10,
b: () => console.log(this.i, this),
c: function() {
console.log( this.i, this)
}
}
obj.b(); // prints undefined, Window
obj.c(); // prints 10, Object {...}
function multiplication(x, y) {
return x * y;
}
const double = multiply.bind(undefined, 2);
const double2 = y => add(2, y);
or how to bring some math to JS
In computer science, partial application (or partial function application) refers to the process of fixing a number of arguments to a function, producing another function of smaller arity.