-
Notifications
You must be signed in to change notification settings - Fork 20
/
brand.mjs
134 lines (107 loc) · 2.79 KB
/
brand.mjs
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
import {assert, assertThrow} from './util'
{
class X {
#x
constructor(value = 0) {
this.#x = value
}
equal(that) {
return this.#x === that.#x
}
}
assert(() => new X(1).equal(new X(1)) === true)
assert(() => new X(1).equal(new X(2)) === false)
// expect false, actual throw
assertThrow(() => new X().equal({}))
}
{
class X {
#x
constructor(value = 0) {
this.#x = value
}
equal(that) {
if (!(that instanceof X)) return false
return this.#x === that.#x
}
}
assert(() => new X().equal({}) === false)
// expect false, actual throw
assertThrow(() => new X().equal({ __proto__: X.prototype }))
// expect true? (see also [prototype inheritence issue](prototype-inheritence), actual throw
assertThrow(() => new X().equal(Object.create(new X())))
// expect true? (see also [proxy transparency issue](proxy-transparency)), actual throw
assertThrow(() => new X().equal(new Proxy(new X(), {})))
}
// {
// class X {
// #x
// constructor(value = 0) {
// this.#x = value
// }
// static [Symbol.hasInstance](instance) {
// try {
// instance.#x
// return true
// } catch {
// return false
// }
// }
// equal(that) {
// if (!(that instanceof X)) return false
// return this.#x === that.#x
// }
// }
// assert(() => new X().equal({}) === false)
// assert(() => new X().equal({ __proto__: X.prototype }) === false)
// class Y extends X {}
// assert(() => new Y() instanceof X === true)
// // expect false, actual true
// assert(() => new X() instanceof Y === true)
// }
// {
// class X {
// #x
// constructor(value = 0) {
// this.#x = value
// }
// static [Symbol.hasInstance](instance) {
// if (!this.prototype.isPrototypeOf(instance)) return false
// try {
// instance.#x
// return true
// } catch {
// return false
// }
// }
// equal(that) {
// if (!(that instanceof X)) return false
// return this.#x === that.#x
// }
// }
// class Y extends X {}
// assert(() => new Y() instanceof X === true)
// assert(() => new X() instanceof Y === false)
// assert(() => new Y(1).equal(new Y(1)) === true)
// assert(() => new Y(1).equal(new Y(2)) === false)
// assert(() => new Y().equal(new X()) === false)
// assert(() => new X().equal(new Y()) === false)
// }
// {
// class Z extends Y {
// #z
// constructor(value, ...args) {
// super(...args)
// this.#z = value
// }
// equal(that) {
// return super.equal(that) && this.#z === that.#z
// }
// }
// assert(() => new Z(1).equal(new Z(1)) === true)
// assert(() => new Z(1).equal(new Z(2)) === false)
// assert(() => new Z(1, 1).equal(new Z(1, 1)) === true)
// assert(() => new Z(1, 1).equal(new Z(1, 2)) === false)
// assert(() => new Y().equal(new Z()) === false)
// assert(() => new Z().equal(new Y()) === false)
// }