-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.js
67 lines (57 loc) · 1.33 KB
/
solution.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
// 28: class - super in constructor (solution)
// To do: make all tests pass, leave the assert lines unchanged!
describe('class', () => {
it('if you `extend` a class, use `super()` to call the parent constructor', () => {
class A {
constructor ( ) {
this.levels = 1;
}
}
class B extends A {
constructor ( ) {
super();
this.levels++;
}
}
assert.equal(new B().levels, 2);
});
it('`super()` may also take params', () => {
class A {
constructor ( startValue=1, addTo=1 ) {
this.counter = startValue + addTo;
}
}
class B extends A {
constructor(...args) {
super(...args);
this.counter++;
}
}
assert.equal(new B(42, 2).counter, 45);
});
it('it is important where you place your `super()` call!', () => {
class A {
inc ( ) {
this.countUp = 1;
}
}
class B extends A {
inc ( ) {
this.countUp = 2;
super.inc();
return this.countUp;
}
}
assert.equal(new B().inc(), 1);
});
it('use `super.constructor` to find out if there is a parent constructor', () => {
class B {}
class A extends B {
constructor() {
super();
this.isTop = !super.constructor;
}
}
assert.equal(new A().isTop, false);
});
});