-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathisinstanceof.js
More file actions
96 lines (66 loc) · 2.92 KB
/
Copy pathisinstanceof.js
File metadata and controls
96 lines (66 loc) · 2.92 KB
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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Copyright (c) ChakraCore Project Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
// @ts-check
function inherit(child, parent) {
child.prototype = Object.create(parent.prototype);
child.prototype.constructor = child;
}
// #region setup "classes"
function ComponentBase() { }
function Component() { }
inherit(Component, ComponentBase);
function Health() { }
inherit(Health, Component);
function ShapeBaseBase() { }
function ShapeBase() { }
inherit(ShapeBase, ShapeBaseBase);
function Shape() { }
inherit(Shape, ShapeBase);
function Box() { }
inherit(Box, Shape);
function Circle() { }
inherit(Circle, Shape);
// #endregion
// #region check functions
function checkA(a, b) {
return a instanceof b;
}
function checkB(a, b) {
return a instanceof b;
}
function checkC(a, b) {
return a instanceof b;
}
function checkD(a, b) {
return a instanceof b;
}
function checkE(a, b) {
return a instanceof b;
}
// #endregion
console.log(':Box:Component:', checkA(Box.prototype, Component));
console.log(':Component:ComponentBase:', checkB(Component.prototype, ComponentBase));
console.log(':Health:Component:', checkC(Health.prototype, Component));
console.log(':Shape:ShapeBase:', checkD(Shape.prototype, ShapeBase));
console.log(':ShapeBase:ShapeBaseBase:', checkE(ShapeBase.prototype, ShapeBaseBase));
console.log(" -> Change prototype");
Object.setPrototypeOf(Shape.prototype, Component.prototype);
// Box inherits Shape (changed) -> should invalidate cache (NoCacheHit)
console.log(':Box:Component:', checkA(Box.prototype, Component), Box.prototype instanceof Component);
// New prototype chain remains valid (CacheHit)
console.log(':Component:ComponentBase:', checkB(Component.prototype, ComponentBase), Component.prototype instanceof ComponentBase);
// Seperate prototype chain remains valid (CacheHit)
console.log(':Health:Component:', checkC(Health.prototype, Component), Health.prototype instanceof Component);
// We changed that! (NoCacheHit)
console.log(':Shape:ShapeBase:', checkD(Shape.prototype, ShapeBase), Shape.prototype instanceof ShapeBase);
// Old prototype chain remains valid (CacheHit)
console.log(':ShapeBase:ShapeBaseBase:', checkE(ShapeBase.prototype, ShapeBaseBase), ShapeBase.prototype instanceof ShapeBaseBase);
console.log("====================");
console.log(':Box:Circle', checkA(Box.prototype, Circle));
console.log(" -> Change prototype");
Object.setPrototypeOf(Circle.prototype, Shape.prototype);
// Modifying the function (constructor) does not invalidate the cache
console.log(':Box:Circle', checkA(Box.prototype, Circle), Box.prototype instanceof Circle);