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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
// Note the globalThisValue and globalObject do not need to be the same.
const globalThisValue = this;
const globalObject = (0, eval)("this");
// These tests are done in global state to ensure that is possible
const globalArrow = () => {
expect(this).toBe(globalThisValue);
return this;
};
function globalFunction() {
expect(this).toBe(globalObject);
expect(globalArrow()).toBe(globalThisValue);
const arrowInGlobalFunction = () => this;
expect(arrowInGlobalFunction()).toBe(globalObject);
return arrowInGlobalFunction;
}
expect(globalArrow()).toBe(globalThisValue);
expect(globalFunction()()).toBe(globalObject);
const arrowFromGlobalFunction = globalFunction();
const customThisValue = {
isCustomThis: true,
variant: 0,
};
const otherCustomThisValue = {
isCustomThis: true,
variant: 1,
};
describe("describe with arrow function", () => {
expect(this).toBe(globalThisValue);
test("nested test with normal function should get global object", function () {
expect(this).toBe(globalObject);
});
test("nested test with arrow function should get same this value as enclosing function", () => {
expect(this).toBe(globalThisValue);
});
});
describe("describe with normal function", function () {
expect(this).toBe(globalObject);
test("nested test with normal function should get global object", function () {
expect(this).toBe(globalObject);
});
test("nested test with arrow function should get same this value as enclosing function", () => {
expect(this).toBe(globalObject);
});
});
describe("basic behavior", () => {
expect(this).toBe(globalThisValue);
expect(arrowFromGlobalFunction()).toBe(globalObject);
expect(customThisValue).not.toBe(otherCustomThisValue);
test("binding arrow function does not influence this value", () => {
const boundGlobalArrow = globalArrow.bind({ shouldNotBeHere: true });
expect(boundGlobalArrow()).toBe(globalThisValue);
});
function functionInArrow() {
expect(arrowFromGlobalFunction()).toBe(globalObject);
return this;
}
function functionWithArrow() {
expect(arrowFromGlobalFunction()).toBe(globalObject);
return () => {
expect(arrowFromGlobalFunction()).toBe(globalObject);
return this;
};
}
function strictFunction() {
"use strict";
return this;
}
test("functions get globalObject as this value", () => {
expect(functionInArrow()).toBe(globalObject);
expect(functionWithArrow()()).toBe(globalObject);
});
test("strict functions get undefined as this value", () => {
expect(strictFunction()).toBeUndefined();
});
test("bound function gets overwritten this value", () => {
const boundFunction = functionInArrow.bind(customThisValue);
expect(boundFunction()).toBe(customThisValue);
const boundFunctionWithArrow = functionWithArrow.bind(customThisValue);
expect(boundFunctionWithArrow()()).toBe(customThisValue);
// However we cannot bind the arrow function itself
const failingArrowBound = boundFunctionWithArrow().bind(otherCustomThisValue);
expect(failingArrowBound()).toBe(customThisValue);
const boundStrictFunction = strictFunction.bind(customThisValue);
expect(boundStrictFunction()).toBe(customThisValue);
});
});
describe("functions on created objects", () => {
const obj = {
func: function () {
expect(arrowFromGlobalFunction()).toBe(globalObject);
return this;
},
funcWithArrow: function () {
expect(arrowFromGlobalFunction()).toBe(globalObject);
return () => this;
},
arrow: () => {
expect(arrowFromGlobalFunction()).toBe(globalObject);
return this;
},
otherProperty: "yes",
};
test("function get this value of associated object", () => {
expect(obj.func()).toBe(obj);
});
test("arrow function on object get above this value", () => {
expect(obj.arrow()).toBe(globalThisValue);
});
test("arrow function from normal function from object has object as this value", () => {
expect(obj.funcWithArrow()()).toBe(obj);
});
test("bound overwrites value of normal object function", () => {
const boundFunction = obj.func.bind(customThisValue);
expect(boundFunction()).toBe(customThisValue);
const boundFunctionWithArrow = obj.funcWithArrow.bind(customThisValue);
expect(boundFunctionWithArrow()()).toBe(customThisValue);
const boundArrowFunction = obj.arrow.bind(customThisValue);
expect(boundArrowFunction()).toBe(globalThisValue);
});
test("also works for object defined in function", () => {
(function () {
expect(arrowFromGlobalFunction()).toBe(globalObject);
// It is bound below
expect(this).toBe(customThisValue);
const obj2 = {
func: function () {
expect(arrowFromGlobalFunction()).toBe(globalObject);
return this;
},
arrow: () => {
expect(arrowFromGlobalFunction()).toBe(globalObject);
return this;
},
otherProperty: "also",
};
expect(obj2.func()).toBe(obj2);
expect(obj2.arrow()).toBe(customThisValue);
}.bind(customThisValue)());
});
});
describe("behavior with classes", () => {
class Basic {
constructor(value) {
expect(this).toBeInstanceOf(Basic);
this.arrowFunctionInClass = () => {
return this;
};
this.value = value;
expect(arrowFromGlobalFunction()).toBe(globalObject);
}
func() {
expect(arrowFromGlobalFunction()).toBe(globalObject);
return this;
}
}
const basic = new Basic(14);
const basic2 = new Basic(457);
expect(basic).not.toBe(basic2);
test("calling functions on class should give instance as this value", () => {
expect(basic.func()).toBe(basic);
expect(basic2.func()).toBe(basic2);
});
test("calling arrow function created in constructor should give instance as this value", () => {
expect(basic.arrowFunctionInClass()).toBe(basic);
expect(basic2.arrowFunctionInClass()).toBe(basic2);
});
test("can bind function in class", () => {
const boundFunction = basic.func.bind(customThisValue);
expect(boundFunction()).toBe(customThisValue);
const boundFunction2 = basic2.func.bind(otherCustomThisValue);
expect(boundFunction2()).toBe(otherCustomThisValue);
});
});
describe("derived classes behavior", () => {
class Base {
baseFunction() {
expect(arrowFromGlobalFunction()).toBe(globalObject);
return this;
}
}
class Derived extends Base {
constructor(value) {
expect(arrowFromGlobalFunction()).toBe(globalObject);
const arrowMadeBeforeSuper = () => {
expect(this).toBeInstanceOf(Derived);
return this;
};
super();
expect(arrowMadeBeforeSuper()).toBe(this);
this.arrowMadeBeforeSuper = arrowMadeBeforeSuper;
this.arrowMadeAfterSuper = () => {
expect(this).toBeInstanceOf(Derived);
return this;
};
this.value = value;
}
derivedFunction() {
expect(arrowFromGlobalFunction()).toBe(globalObject);
return this;
}
}
test("can create derived with arrow functions using this before super", () => {
const testDerived = new Derived(-89);
expect(testDerived.arrowMadeBeforeSuper()).toBe(testDerived);
expect(testDerived.arrowMadeAfterSuper()).toBe(testDerived);
});
test("base and derived functions get correct this values", () => {
const derived = new Derived(12);
expect(derived.derivedFunction()).toBe(derived);
expect(derived.baseFunction()).toBe(derived);
});
test("can bind derived and base functions", () => {
const derived = new Derived(846);
const boundDerivedFunction = derived.derivedFunction.bind(customThisValue);
expect(boundDerivedFunction()).toBe(customThisValue);
const boundBaseFunction = derived.baseFunction.bind(otherCustomThisValue);
expect(boundBaseFunction()).toBe(otherCustomThisValue);
});
});
describe("proxy behavior", () => {
test("with no handler it makes no difference", () => {
const globalArrowProxyNoHandler = new Proxy(globalArrow, {});
expect(globalArrowProxyNoHandler()).toBe(globalThisValue);
});
test("proxy around global arrow still gives correct this value", () => {
let lastThisArg = null;
const handler = {
apply(target, thisArg, argArray) {
expect(target).toBe(globalArrow);
lastThisArg = thisArg;
expect(this).toBe(handler);
return target(...argArray);
},
};
const globalArrowProxy = new Proxy(globalArrow, handler);
expect(globalArrowProxy()).toBe(globalThisValue);
expect(lastThisArg).toBeUndefined();
const boundProxy = globalArrowProxy.bind(customThisValue);
expect(boundProxy()).toBe(globalThisValue);
expect(lastThisArg).toBe(customThisValue);
expect(globalArrowProxy.call(15)).toBe(globalThisValue);
expect(lastThisArg).toBe(15);
});
});
describe("derived classes which access this before super should fail", () => {
class Base {}
test("direct access of this should throw reference error", () => {
class IncorrectConstructor extends Base {
constructor() {
this.something = "this will fail";
super();
}
}
expect(() => {
new IncorrectConstructor();
}).toThrowWithMessage(ReferenceError, "|this| has not been initialized");
});
test("access of this via a arrow function", () => {
class IncorrectConstructor extends Base {
constructor() {
const arrow = () => this;
arrow();
super();
}
}
expect(() => {
new IncorrectConstructor();
}).toThrowWithMessage(ReferenceError, "|this| has not been initialized");
});
test("access of this via a eval", () => {
class IncorrectConstructor extends Base {
constructor() {
eval("this.foo = 'bar'");
super();
}
}
expect(() => {
new IncorrectConstructor();
}).toThrowWithMessage(ReferenceError, "|this| has not been initialized");
});
test.skip("access of this via a eval in arrow function", () => {
class IncorrectConstructor extends Base {
constructor() {
const arrow = () => eval("() => this")();
arrow();
super();
}
}
expect(() => {
new IncorrectConstructor();
}).toThrowWithMessage(ReferenceError, "|this| has not been initialized");
});
test("access of this via arrow function even if bound with something else", () => {
class IncorrectConstructor extends Base {
constructor() {
const arrow = () => this;
const boundArrow = arrow.bind(customThisValue);
boundArrow();
super();
}
}
expect(() => {
new IncorrectConstructor();
}).toThrowWithMessage(ReferenceError, "|this| has not been initialized");
});
});
describe("with statements", () => {
test("this value is still the global object", () => {
const obj = { haveValue: true, hello: "friends" };
with (obj) {
expect(this).toBe(globalThisValue);
expect(hello).toBe("friends");
}
});
test("with gets this value form outer scope", () => {
const obj = { haveValue: true, hello: "friends" };
function callme() {
with (obj) {
expect(this).toBe(customThisValue);
expect(hello).toBe("friends");
}
}
const boundMe = callme.bind(customThisValue);
boundMe();
});
});
describe("in non strict mode primitive this values are converted to objects", () => {
const array = [true, false];
// Technically the comma is implementation defined here. (Also for tests below.)
expect(array.toLocaleString()).toBe("true,false");
test("directly overwriting toString", () => {
let count = 0;
Boolean.prototype.toString = function () {
count++;
return typeof this;
};
expect(array.toLocaleString()).toBe("object,object");
expect(count).toBe(2);
});
test("overwriting toString with a getter", () => {
let count = 0;
Object.defineProperty(Boolean.prototype, "toString", {
get() {
count++;
const that = typeof this;
return function () {
return that;
};
},
});
expect(array.toLocaleString()).toBe("object,object");
expect(count).toBe(2);
});
});
|