-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclouser.js
285 lines (232 loc) · 6.48 KB
/
clouser.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/**
* A closure is the combination of a function and
* the lexical environment within which that function was declared.
*
* lexical scope : inner functions contain the scope of parent functions even
* if the parent function has returned.
*/
function init() {
var name = 'Mozilla'; // name is a local variable created by init
function displayName() { // displayName() is the inner function, a closure
console.log(name); // use variable declared in the parent function
}
displayName();
}
init();
//////////////////////////////////////////
function makeFunc() {
var name = 'Mozilla';
function displayName() {
console.log(name);
}
return displayName;
}
var myFunc = makeFunc();
myFunc();
///////////////////////////////////////////////
function makeAdder(x) {
return function (y) {
return x + y;
};
}
var add5 = makeAdder(5);
var add10 = makeAdder(10);
console.log(add5(2)); // 7
console.log(add10(2)); // 12
///////////////////////////////////////////////
var addTwo = function (outer) {
var add = function (inner) {
return outer + inner;
}
return add;
}
console.log(addTwo(5)); // [Function: add]
var addThree = new addTwo(5);
var addFour = new addTwo(5);
console.log(addThree(6)); // 11
console.log(addFour(-5)); // 0
///////////////// Emulating private methods with closures /////////////////////
var counter = (function () {
var privateCounter = 0;
function changeBy(val) {
privateCounter += val;
}
return {
increment: function () {
changeBy(1);
},
decrement: function () {
changeBy(-1);
},
value: function () {
return privateCounter;
}
};
})();
console.log(counter.value()); // logs 0
counter.increment();
counter.increment();
console.log(counter.value()); // logs 2
counter.decrement();
console.log(counter.value()); // logs 1
///////////////////////////////////////////////
/**
* Notice how each of the two counters, counter1 and counter2, maintains its independence from the other.
* Each closure references a different version of the privateCounter variable through its own closure.
* Each time one of the counters is called, its lexical environment changes by changing the value of this variable;
* however changes to the variable value in one closure do not affect the value in the other closure.
*/
/**
* Using closures in this way provides a number of benefits that are normally associated with object-oriented programming
* -- in particular, data hiding and encapsulation.
*/
var makeCounter = function () {
var privateCounter = 0;
function changeBy(val) {
privateCounter += val;
}
return {
increment: function () {
changeBy(1);
},
decrement: function () {
changeBy(-1);
},
value: function () {
return privateCounter;
}
}
};
var counter1 = makeCounter();
var counter2 = makeCounter();
console.log(counter1.value()); /* Alerts 0 */
counter1.increment();
counter1.increment();
console.log(counter1.value()); /* Alerts 2 */
counter1.decrement();
console.log(counter1.value()); /* Alerts 1 */
console.log(counter2.value()); /* Alerts 0 */
//////////////////// Closure Scope Chain ////////////////////////////////
// For every closure we have three scopes:-
// Local Scope ( Own scope)
// Outer Functions Scope
// Global Scope
/**
* DELL EMC Question
* someFunction('Hi')('Javascript Guy,')('You Are Awesome!');
* @Output : Hi Javascript Guys, You Are Awesome!
*/
function appendString(str1) {
return function (str2) {
return function (str3) {
return (str1 + " " + str2 + " " + str3);
}
}
}
console.log(appendString('Hi')('Javascript Guy,')('You Are Awesome!'));
//////////////////////////////////////////////////
// global scope
var e = 10;
function sum(a) {
return function (b) {
return function (c) {
// outer functions scope
return function (d) {
// local scope
return a + b + c + d + e;
}
}
}
}
console.log(sum(1)(2)(3)(4)); // log 20
// We can also write without anonymous functions:
// global scope
var e = 10;
function sum(a) {
return function sum2(b) {
return function sum3(c) {
// outer functions scope
return function sum4(d) {
// local scope
return a + b + c + d + e;
}
}
}
}
var s = sum(1);
var s1 = s(2);
var s2 = s1(3);
var s3 = s2(4);
console.log(s3) //log 20
////////////////// Performance considerations ////////////////////////
/**
* For instance, when creating a new object/class, methods should normally be associated to the object's prototype rather
* than defined into the object constructor.
* The reason is that whenever the constructor is called,
* the methods would get reassigned (that is, for every object creation).
*/
function MyObject(name, message) {
this.name = name.toString();
this.message = message.toString();
this.getMessage = function () {
return this.message;
}
this.getName = function () {
return this.name;
}
}
var obj = new MyObject("MyObject", "Hello ");
console.log(obj.getMessage() + " " + obj.getName()); // Hello MyObject
console.log(obj.name); // MyObject
/*Above code does not take advantage of the benefits of closures,
ie. No private variables are being used in the public functions.
So we could instead rewrite it as follows:
*/
function MyObject2(name, message) {
this.name = name.toString();
this.message = message.toString();
}
MyObject2.prototype = {
getMessage: function () {
return this.message;
},
getName: function () {
return this.name;
}
}
var obj2 = new MyObject2("MyObject2", "Hello ");
console.log(obj2.getMessage() + " " + obj2.getName());
/*
It is not a good practice to update the prototype itself.
So Instead we need to append these functions to the prototype.
*/
function MyObject3(name, message) {
this.name = name.toString();
this.message = message.toString();
}
MyObject3.prototype.getMessage = function () {
return this.message;
}
MyObject3.prototype.getName = function () {
return this.name;
}
var obj3 = new MyObject3("MyObject3", "Hello ");
console.log(obj3.getMessage() + " " + obj3.getName());
/*
The same can be re-writted with IIF.
*/
function MyObject4(name, message) {
this.name = name.toString();
this.message = message.toString();
}
(function () {
this.getMessage = function (data) {
console.log(this);
return this.message + data;
}
this.getName = function () {
return this.name;
}
}).call(MyObject4.prototype);
var obj4 = new MyObject4("MyObject4", "Hello ");
console.log(obj4.getMessage() + " " + obj4.getName());