-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsection12.js
74 lines (60 loc) · 1.55 KB
/
section12.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//TODO functional expressions
//TODO functional expressions and functional declarations
function myFn(a, b) {
// functional declaration has a name
let c;
a = a + 1;
c = a + b;
return c;
}
const anonymousFn = function (x, y) {
// functional expression does not have its own name. Can be used only in the declared variable
let z;
x = x + 1;
z = x + y;
return z;
};
// TODO Arrow functions
const arrowFn = (europe, asia) => {
// also does not have its own name
let japan;
asia = true;
europe = false;
japan = europe || asia;
return japan;
};
setTimeout(() => {
console.log("Delayed message");
}, 2000);
myFn(1, 2);
anonymousFn(5, 4);
arrowFn(true, false);
// TODO how to shorten arrow functions
(abc, cba) => a + b; //if there is only 1 expression in a function You may remove the {} and return. Implecit return
//TODO values by default
const defaultValueFn = (value, multiplier = 1) => {
// "multiplier" already has value of 1 by default
return value * multiplier;
};
defaultValueFn(3, 3);
defaultValueFn(3);
//TODO Implicit return of the object
const newPost = (post, addedAt = Date()) => ({ // "Date" is JavaScript function which returns current date and time while calling
...post,
// addedPost: addedPost,
addedAt,
})
const firstPost = {
id: 1,
author: 'Chareles Duhigg'
}
newPost(firstPost)
//TODO explicit return of the object
const newPost2 = (post, addedAt = Date()) => {
return { //now we are using "return"
...post,
// addedPost: addedPost,
addedAt,
}
}
newPost2(firstPost)