-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathexamples.js
72 lines (55 loc) · 1.33 KB
/
examples.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
/*
Closures are the combination of an inner function with the variables in the surround scope
A way to preserve variables inside of functions
*/
function makeCounter() {
let count = 0;
function incrementCount() {
count++;
console.log(count);
}
return incrementCount;
}
const counter1 = makeCounter();
counter1() // 1
counter1() // 2
counter1() // 3
const counter2 = makeCounter();
counter2(); // 1
counter2(); // 2
counter1(); // 4
function makeFriendList() {
const _friends = [];
return {
// _friends: [],
addFriends(name) {
// this._friends.push(name)
debugger;
_friends.push(name);
},
getFriends() {
return [..._friends];
}
}
}
const bensFriends = makeFriendList();
bensFriends.addFriends("Destiny")
bensFriends.addFriends("Amanda")
bensFriends.addFriends("Maya")
const friendsList = bensFriends.getFriends();
friendsList.pop();
friendsList.pop();
console.log(bensFriends.getFriends());
function makeGreeter(name) {
// 1st Invocation: let name = "Ben"
// 2nd Invocation: let name = "Maya"
function greet() {
debugger;
console.log(`Hello ${name}`);
}
return greet;
}
const greeter1 = makeGreeter("Ben")
const greeter2 = makeGreeter("Maya");
greeter1();
greeter2();