-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathunit-4-2.js
129 lines (101 loc) · 2.96 KB
/
unit-4-2.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
console.log("closures!");
// const user = {
// name: "ben spector",
// email: "ben@marcylabschool.org",
// friends: [
// "motun", "carmen"
// ],
// addFriend(name) {
// this.friends.push(name)
// },
// printThis() {
// console.log(this)
// }
// }
// user.addFriend("Vinny");
// user.printThis();
// function makeVault(code) {
// return {
// _secretCode: code, // _ means that this is a "private value" and shouldn't be changeds
// open: function(code) {
// if (code === this._secretCode) {
// console.log("access granted")
// }
// else {
// console.log("access denied")
// }
// }
// }
// }
// const myVault = makeVault(12345);
// myVault.open(12345);
// function getRandomPrinter() { // outer function
// // a closure is created around this variable
// const randomValue = Math.random();
// console.log(`creating a random printer for ${randomValue}`);
// function printRandom() { // inner function
// console.log(randomValue);
// // this reference saves a reference to randomValue that would otherwise be lost when the function finishes
// };
// // the returned function will retain a reference to the closure variables
// return printRandom;
// }
// const randomPrinter1 = getRandomPrinter();
// randomPrinter1();
// randomPrinter1();
// randomPrinter1();
// const randomPrinter2 = getRandomPrinter();
// randomPrinter2();
// function makeCounter() {
// // a closure is created "around" this variable
// let count = 0;
// // the returned object will retain a reference to count, even after it is returned from the function.
// return {
// getCount() {
// return count;
// },
// increment() {
// count = count + 1;
// }
// }
// }
// // each instance has its own count value
// const counter1 = makeCounter();
// const counter2 = makeCounter();
// counter1.increment();
// counter1.increment();
// counter1.increment();
// counter2.increment();
// console.log(counter1.getCount()); // 3
// console.log(counter2.getCount()); // 1
function makeVault(code) {
let _secretCode = code;
return {
open: function(code) {
if (code === _secretCode) {
console.log("access granted")
}
else {
console.log("access denied")
}
}
}
}
const myVault = makeVault(12345);
myVault.open(12345);
const myVault2 = makeVault("hello");
myVault2.open("hello");
function makeContacts () {
const contactList = [];
return {
getContacts () {
console.log(contactList);
},
addContact (contact) {
contactList.push(contact);
},
deleteFriend (friend) {
contactList.filter((index) => index != friend);
}
};
}