forked from imteekay/functional-programming-learning-path
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintro_to_js.js
More file actions
39 lines (30 loc) · 798 Bytes
/
Copy pathintro_to_js.js
File metadata and controls
39 lines (30 loc) · 798 Bytes
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
// const instead of let and var most of the time
const number = 1;
const arr = [1, 2, 3];
// `const obj = { a: a }` but we have a better way to do it
const a = "a";
const objA = { a };
const b = "b";
const objB = { b };
// composing objects with object spread operator
const ab = { ...objA, ...objB }; // { a: 'a', b: 'b' }
// Destructuring arrays
const [a, b] = ["a", "b"];
a; // 'a'
b; // 'b'
// destructuring objects
const { one } = { one: 1 };
one; // 1
// but also do multiple destructuring
const action = { type: "SUM", data: 1 };
const { type, data } = action;
// we can use this technique in reducers
const reducer = (state = 0, action = {}) => {
const { type, data } = action;
switch (type) {
case "SUM":
return state + data;
default:
return state;
}
};