| 1 |
|
| 2 |
var count = 0; |
| 3 |
|
| 4 |
function Parent() { |
| 5 |
// Scanning "this" properties here with Object.keys() solved the bug in my case |
| 6 |
//Object.keys(this); |
| 7 |
|
| 8 |
this.log('Parent ctor'); |
| 9 |
this.meth1(); |
| 10 |
this.log('data3 before : ' + this.data3); |
| 11 |
this.meth2(); |
| 12 |
// Added properties lost in ChildA |
| 13 |
this.log('data3 after : ' + this.data3); |
| 14 |
this.log(''); |
| 15 |
|
| 16 |
if (count++) |
| 17 |
assertEq(this.data3, 'z'); |
| 18 |
} |
| 19 |
Parent.prototype.meth1 = function () { |
| 20 |
this.log('Parent.meth1()'); |
| 21 |
}; |
| 22 |
Parent.prototype.meth2 = function () { |
| 23 |
this.log('Parent.meth2()'); |
| 24 |
// Requirement for the bug : Parent.meth2() needs to add data |
| 25 |
this.data4 = 'x'; |
| 26 |
}; |
| 27 |
Parent.prototype.log = function (data) { |
| 28 |
print(data); |
| 29 |
} |
| 30 |
|
| 31 |
// Intermediate constructor to instantiate children prototype without executing Parent constructor code |
| 32 |
function ParentEmptyCtor() { } |
| 33 |
ParentEmptyCtor.prototype = Parent.prototype; |
| 34 |
|
| 35 |
function ChildA() { |
| 36 |
this.log('ChildA ctor'); |
| 37 |
Parent.call(this); |
| 38 |
} |
| 39 |
ChildA.prototype = new ParentEmptyCtor(); |
| 40 |
// Using Object.create() instead solves the bug |
| 41 |
//ChildA.prototype = Object.create(ParentEmptyCtor.prototype); |
| 42 |
ChildA.prototype.constructor = ChildA; |
| 43 |
ChildA.prototype.meth1 = function () { |
| 44 |
this.log('ChildA.meth1()'); |
| 45 |
this.data3 = 'z'; |
| 46 |
}; |
| 47 |
ChildA.prototype.meth2 = function () { |
| 48 |
this.log('ChildA.meth2()'); |
| 49 |
}; |
| 50 |
|
| 51 |
function ChildB() { |
| 52 |
this.log('ChildB ctor'); |
| 53 |
Parent.call(this); |
| 54 |
} |
| 55 |
ChildB.prototype = new ParentEmptyCtor(); |
| 56 |
//ChildB.prototype = Object.create(ParentEmptyCtor.prototype); |
| 57 |
ChildB.prototype.constructor = ChildB; |
| 58 |
|
| 59 |
function demo() { |
| 60 |
// Requirement for the bug : ChildB needs to be instantiated before ChildA |
| 61 |
new ChildB(); |
| 62 |
new ChildA(); |
| 63 |
} |
| 64 |
demo(); |