28. JavaScript Fundamentals
Arrow Functions:
function foo() {
return 10;
}
foo = () => 10;
obj = {
id: 42,
foo: function foo() {
setTimeout(() => {
console.log(this.id);
}, 1000);
}
};
((foo, p, list1, list2 ) => {
(foo = (x, y = x * 2) =>
function bar(z, baz, obj) {
if (z.length > 3) {
return z.map( baz = v =>
v > 3 ? v + y : baz(v * 4)
);
}
else {
obj = [];
setTimeout( () =>
obj.length = 1, obj[0] = this.w, 100);
return obj;
}
},
p = foo(2),
list1 = [1, 3, 4],
list2 = list1.concat(6),
list1 = p.call({ w: 42 }, list1),
list2 = p(list2),
setTimeout( () => console.log(list1[0] === list2.reduce((s, v) => s + v, 0)), 200))
})();
Let:
function foo(x, y) {
try {
var z = bar(x * 10);
}
catch(err) {
}
if ( x > y) {
let tmp = x;
x = y;
y = tmp;
}
for (let i = 0; i < 10; i++) {
console.log(i);
}
return {'x': x, 'y': y };
}
function bar(a) {
return a * 100;
}
Const:
const x = 10;
const PI = 3.14;
const y = Object.freeze([0, 1, 2]);
const x = 2;
const fns = [];
{
const x = 5;
for (let i = 0; i < x; i++) {
fns[i] = (num) => i;
}
}
console.log( (x * 2) === fns[x*2]());
Default Values:
function foo(x = 42) {
return x;
}
Lazy Expressions:
function bar() {
console.log('!!!');
}
function foo(x = bar()) {
}
foo();
foo();
foo();