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