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
|
test("method inheritance", () => {
class Parent {
method() {
return 3;
}
}
class Child extends Parent {}
const p = new Parent();
const c = new Child();
expect(p.method()).toBe(3);
expect(c.method()).toBe(3);
});
test("method overriding", () => {
class Parent {
method() {
return 3;
}
}
class Child extends Parent {
method() {
return 10;
}
}
const p = new Parent();
const c = new Child();
expect(p.method()).toBe(3);
expect(c.method()).toBe(10);
});
test("parent method reference with super", () => {
class Parent {
method() {
return 3;
}
}
class Child extends Parent {
method() {
return super.method() * 2;
}
}
const p = new Parent();
const c = new Child();
expect(p.method()).toBe(3);
expect(c.method()).toBe(6);
});
test("child class access to parent class initialized properties", () => {
class Parent {
constructor() {
this.x = 3;
}
}
class Child extends Parent {}
const p = new Parent();
const c = new Child();
expect(p.x).toBe(3);
expect(c.x).toBe(3);
});
test("child class modification of parent class properties", () => {
class Parent {
constructor() {
this.x = 3;
}
}
class Child extends Parent {
change() {
this.x = 10;
}
}
const p = new Parent();
const c = new Child();
expect(p.x).toBe(3);
expect(c.x).toBe(3);
c.change();
expect(c.x).toBe(10);
});
test("inheritance and hasOwnProperty", () => {
class Parent {
constructor() {
this.x = 3;
}
}
class Child extends Parent {
method() {
this.y = 10;
}
}
const p = new Parent();
const c = new Child();
expect(p.hasOwnProperty("x")).toBeTrue();
expect(p.hasOwnProperty("y")).toBeFalse();
expect(c.hasOwnProperty("x")).toBeTrue();
expect(c.hasOwnProperty("y")).toBeFalse();
c.method();
expect(c.hasOwnProperty("x")).toBeTrue();
expect(c.hasOwnProperty("y")).toBeTrue();
});
test("super constructor call from child class with argument", () => {
class Parent {
constructor(x) {
this.x = x;
}
}
class Child extends Parent {
constructor() {
super(10);
}
}
const p = new Parent(3);
const c = new Child(3);
expect(p.x).toBe(3);
expect(c.x).toBe(10);
});
test("advanced 'extends' RHS", () => {
const foo = {
bar() {
return {
baz() {
return function () {
return function () {
return { quux: Number };
};
};
},
};
},
};
class Foo extends foo.bar()["baz"]()`qux`().quux {}
expect(new Foo()).toBeInstanceOf(Number);
});
test("issue #7045, super constructor call from child class in catch {}", () => {
class Parent {
constructor(x) {
this.x = x;
}
}
class Child extends Parent {
constructor() {
try {
throw new Error("Error in Child constructor");
} catch (e) {
super(e.message);
}
}
}
const c = new Child();
expect(c.x).toBe("Error in Child constructor");
});
test("Issue #7044, super property access before super() call", () => {
class Foo {
constructor() {
super.bar;
}
}
new Foo();
});
test("Issue #8574, super property access before super() call", () => {
var hit = false;
class Foo extends Object {
constructor() {
expect(() => {
const foo = super.bar();
}).toThrowWithMessage(ReferenceError, "|this| has not been initialized");
hit = true;
}
}
// Note: We catch two exceptions here.
expect(() => {
new Foo();
}).toThrowWithMessage(ReferenceError, "|this| has not been initialized");
expect(hit).toBeTrue();
});
|