blob: f253d6024ceebdc4d78c71250013fa1e4159ae0c (
plain)
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
|
var count = 0;
function Parent() {
// Scanning "this" properties here with Object.keys() solved the bug in my case
//Object.keys(this);
this.log('Parent ctor');
this.meth1();
this.log('data3 before : ' + this.data3);
this.meth2();
// Added properties lost in ChildA
this.log('data3 after : ' + this.data3);
this.log('');
if (count++)
assertEq(this.data3, 'z');
}
Parent.prototype.meth1 = function () {
this.log('Parent.meth1()');
};
Parent.prototype.meth2 = function () {
this.log('Parent.meth2()');
// Requirement for the bug : Parent.meth2() needs to add data
this.data4 = 'x';
};
Parent.prototype.log = function (data) {
print(data);
}
// Intermediate constructor to instantiate children prototype without executing Parent constructor code
function ParentEmptyCtor() { }
ParentEmptyCtor.prototype = Parent.prototype;
function ChildA() {
this.log('ChildA ctor');
Parent.call(this);
}
ChildA.prototype = new ParentEmptyCtor();
// Using Object.create() instead solves the bug
//ChildA.prototype = Object.create(ParentEmptyCtor.prototype);
ChildA.prototype.constructor = ChildA;
ChildA.prototype.meth1 = function () {
this.log('ChildA.meth1()');
this.data3 = 'z';
};
ChildA.prototype.meth2 = function () {
this.log('ChildA.meth2()');
};
function ChildB() {
this.log('ChildB ctor');
Parent.call(this);
}
ChildB.prototype = new ParentEmptyCtor();
//ChildB.prototype = Object.create(ParentEmptyCtor.prototype);
ChildB.prototype.constructor = ChildB;
function demo() {
// Requirement for the bug : ChildB needs to be instantiated before ChildA
new ChildB();
new ChildA();
}
demo();
|