-
Notifications
You must be signed in to change notification settings - Fork 167
/
Chapter7(MemoryManagement).js
184 lines (149 loc) · 3.67 KB
/
Chapter7(MemoryManagement).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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
function memory(){
return '';
}
var foo = {
bar1: memory(), // 5kb
bar2: memory() // 5kb
}
function clickEvent(){
alert(foo.bar1[0]);
}
// DOM leak:
var one = document.getElementById("one");
var two = document.getElementById("two");
one.addEventListener('click', function(){
two.remove();
console.log(two); // will print the html even after deletion
});
// fix for above
var one = document.getElementById("one");
one.addEventListener('click', function(){
var two = document.getElementById("two");
two.remove();
});
// unregister the callback
var one = document.getElementById("one");
one.addEventListener('click', function(){
var two = document.getElementById("two");
two.remove();
});
one.removeEventListener('click');
var a = "apples"; //global with var
b = "oranges"; //global without var
console.log(window.a); // prints "apples"
console.log(window.b); // prints "oranges"
var test = {
prop1: 'test'
}
function printProp1(test){
console.log(test.prop1);
}
printProp1(test); //'test'
var test = {
prop1: 'test'
}
function printProp1(prop1){
console.log(prop1);
}
printProp1(test.prop1); //'test'
var test = {
prop1: 'test'
}
console.log(test.prop1); // 'test'
delete test.prop1;
console.log(test.prop1); // _undefined_
// Exercises
function someLargeArray() {
return new Array(1000000);
}
var exampleObject = {
'prop1': someLargeArray(),
'prop2': someLargeArray()
}
function printProperty(obj){
console.log(obj['prop1']);
}
printProperty(exampleObject);
// --
function someLargeArray() {
return new Array(1000000);
}
var exampleObject = {
'prop1': someLargeArray(),
'prop2': someLargeArray()
}
function printProperty(prop){
console.log(prop);
}
printProperty(exampleObject['prop1']);
// -- Question 2
var RED = 0,
GREEN = 1,
BLUE = 2;
function redGreenBlueCount(arr) {
var counter = new Array(3).fill(0);
for (var i=0; i < arr.length; i++) {
var curr = arr[i];
if (curr == RED) {
counter[RED]++;
} else if (curr == GREEN) {
counter[GREEN]++;
} else if (curr == BLUE) {
counter[BLUE]++;
}
}
return counter;
}
function redGreenBlueCount(arr) {
var RED = 0,
GREEN = 1,
BLUE = 2,
counter = new Array(3).fill(0);
for (var i=0; i < arr.length; i++) {
var curr = arr[i];
if (curr == RED) {
counter[RED]++;
} else if (curr == GREEN) {
counter[GREEN]++;
} else if (curr == BLUE) {
counter[BLUE]++;
}
}
return counter;
}
//
//<button id="one">Button 1</button>
//<button id="two">Button 2</button>
// Question:
var one = document.querySelector("#one");getElementById("one");
var two = document.querySelector("#two");getElementById("two");
function callBackExample () {
one.removeEventListener("",callBackExample);
}
one.addEventListener('click' , function(){
two.remove();
console.log(two); // will print the html even after deletion
});
two.addEventListener('click', function(){
one.remove();
console.log(one); // will print the html even after deletion
});
// answer:
var one = document.querySelector("#one");
var two = document.querySelector("#two");
function callbackOne() {
var two = document.querySelector("#two");
if (!two)
return;
two.remove();
one.removeEventListener("hover", callbackOne);
}
function callbackTwo() {
var one = document.querySelector("#one");
if (!one)
return;
one.remove();
two.removeEventListener("hover", callbackTwo);
}
one.addEventListener("click", callbackOne);
two.addEventListener("click", callbackTwo);