-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjectTest.js
281 lines (210 loc) · 8.53 KB
/
objectTest.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
// requires object.js
( () => {
const ok = [];
const myObject = {
a : 1,
// foo : function() { return this.a }
foo() { return this.a } // syntactic sugar
};
ok.push(myObject.foo() === 1);
const myFunctions = [ myObject.foo ];
ok.push(myFunctions[0]() !== 1); // do functions capture "this" in their scope ?
ok.push(myFunctions[0]() === undefined);
myFunctions.a = 2; // "this" refers to the array
ok.push(myFunctions[0]() === 2);
function outer( callback ) {
const a = 2;
return callback();
}
function inner( ) {
const a = 3;
return this.a;
}
ok.push(outer( inner ) === undefined); // guess what
ok.push(outer( inner ) !== 2);
ok.push(outer( inner ) !== 3);
window.a = 4; // global scope
ok.push(outer( inner ) === 4);
report("this-is-an-issue", ok);
}) ();
// This is kind of a silly variant for encoding objects that makes
// use of the fact that a function is just an object and thus has
// an associated dynamic scope.
// The use of "this" is mandatory in all "methods".
// Forgetting to "instantiate" with "new" will cause all kinds of
// trouble.
( () => {
const ok = [];
function Person(first, last) {
this.firstname = first;
this.lastname = last;
this.getName = function() { return this.firstname + " " + this.lastname };
return this;
}
// remember: calling a function retains the scope
const good = Person("Good", "Boy"); // "accidentally" forgot the "new"
ok.push( good.getName() === "Good Boy");
const other = Person("Other", "Boy");
ok.push(other.getName() === "Other Boy");
ok.push(good.getName() === "Other Boy"); // OOPS! We have accidentally overwritten the good boy.
ok.push(false === good instanceof Person); // they do not share the prototype
const good2 = new Person("Good", "Boy"); // one way or the other we have to create a "new" object!
ok.push( good2.getName() === "Good Boy");
const other2 = new Person("Other", "Boy");
ok.push(other2.getName() === "Other Boy");
ok.push(good2.getName() === "Good Boy"); // retained
ok.push(good2 instanceof Person); // now they do
report("object-silly-scope", ok);
}) ();
// Using object literals as a replacement for functions
// is super dynamic, keeps "methods" close to their data,
// but doesn't allow for sharing of structure.
// (unless advanced use with Object.create)
// Also: use of "this" can lead to surprises.
( () => {
const ok = [];
const good = {
firstname : "Good",
lastname : "Boy",
// must use "this" or type error, cannot use arrow function!
getName : function() { return this.firstname + " " + this.lastname }
};
ok.push(good.getName() === "Good Boy");
// share object instance
const other = good;
ok.push(good.getName() === "Good Boy");
// change value
other.firstname = "Other";
ok.push(other.getName() === "Other Boy");
ok.push(good.getName() === "Other Boy");
const store = {
accessor : good.getName // when we store a reference elsewhere
};
ok.push(store.accessor() === "undefined undefined"); // OOPS!
report("object-literal", ok);
}) ();
// Variant that doesn't need to be called with "new"
// since a new object is created with ever "ctor" call.
( () => {
const ok = [];
function OpenPerson(first, last) {
return {
firstname : first,
lastname : last,
// must use "this" or type error, cannot use arrow function!
getName : function() { return this.firstname + " " + this.lastname }
}
}
const good = OpenPerson("Good", "Boy");
ok.push(good.getName() === "Good Boy");
// share object instance
const other = good;
ok.push(good.getName() === "Good Boy");
// change value
other.firstname = "Other";
ok.push(other.getName() === "Other Boy");
ok.push(good.getName() === "Other Boy");
report("object-self-new", ok);
}) ();
// A safe version that makes use of the fact that closure
// scope is safe from manipulation.
// Needs no "this"!
// Trying to change the state fails silently.
( () => {
const ok = [];
function Person(first, last) {
const firstname = first;
const lastname = last;
return {
// cannot use "this" as it is undefined
getName : () => firstname + " " + lastname
}
}
const good = Person("Good", "Boy");
ok.push(good.getName() === "Good Boy");
// change value (failed attempt)
good.firstname = "Bad";
ok.push(good.firstname === "Bad"); // a new value has been set, but it is not used, Object.seal() prevents this
ok.push(good.getName() === "Good Boy"); // change silently swallowed, expected: "Bad Boy"
report("object-failed", ok);
}) ();
// A safe version that makes use of the fact that closure
// scope is safe from manipulation.
// Needs no "this"!
// Creates no "type".
( () => {
const ok = [];
function Person(first, last) {
const firstname = first; // optional, see distinct2
const lastname = last;
return {
getName : () => firstname + " " + lastname
}
}
const good = Person("Good", "Boy");
const bad = Person("Bad", "Boy"); // distinct new instance
ok.push(good.getName() === "Good Boy");
ok.push(bad.getName() === "Bad Boy" );
good.getName = () => "changed";
ok.push(good.getName() === "changed"); // change one instance doesn't change the other
ok.push(bad.getName() === "Bad Boy" );
ok.push(! Person.prototype.isPrototypeOf(good)); // they do not even share the same prototype
ok.push(! Person.prototype.isPrototypeOf(bad));
ok.push(false === good instanceof Person); // good is not a Person!
ok.push("object" === typeof good );
report("object-distinct", ok);
}) ();
// Version of "distinct" that makes use of the closure scope for arguments
( () => {
const ok = [];
function Person(first, last) { // closure scope for arguments
return {
getName : () => first + " " + last
}
}
const good = Person("Good", "Boy");
const bad = Person("Bad", "Boy"); // distinct new instance
ok.push(good.getName() === "Good Boy");
ok.push(bad.getName() === "Bad Boy" );
good.getName = () => "changed";
ok.push(good.getName() === "changed"); // change one instance doesn't change the other
ok.push(bad.getName() === "Bad Boy" );
ok.push(! Person.prototype.isPrototypeOf(good)); // they do not even share the same prototype
ok.push(! Person.prototype.isPrototypeOf(bad));
ok.push(false === good instanceof Person); // good is not a Person!
ok.push("object" === typeof good );
report("object-distinct2", ok);
}) ();
// Standard Typescript way of creating objects (unless with => syntax)
// Still dynamic: instance and "class" (prototype) can change at runtime.
( () => {
const ok = [];
const Person = ( () => { // lexical scope for construction
function Person(first, last) { // constructor, setting up the binding
this.firstname = first;
this.lastname = last;
}
Person.prototype.getName = function() { // functions are shared through the prototype // "=>" not allowed!
return this.firstname + " " + this.lastname;
};
return Person;
}) (); // IIFE
const good = new Person("Good", "Boy"); // now it requires "new"
const bad = new Person("Bad", "Boy"); // distinct new instance
ok.push(good.getName() === "Good Boy"); // without "new" it throws TypeError
ok.push(bad.getName() === "Bad Boy" );
ok.push(good.firstname === "Good"); // the function scope is still accessible for manipulation
good.getName = () => "changed";
ok.push(good.getName() === "changed"); // one can still change a single instance
ok.push(bad.getName() === "Bad Boy" );
ok.push(Person.prototype.isPrototypeOf(good)); // Now they share the same prototype
ok.push(Person.prototype.isPrototypeOf(bad));
// new functions get shared
Person.prototype.secret = () => "top secret!";
ok.push(good.secret() === "top secret!");
ok.push(bad.secret() === "top secret!");
ok.push(good instanceof Person === true);
ok.push(good instanceof Function === false); // why this ???
ok.push(good instanceof Object === true);
report("object-prototype", ok);
}) ();