jsnewman
jsnewman

Reputation: 339

array inheritance question in javascript

function ClassA() { this.a=[]; this.aa=100; }
function ClassB() {  }

ClassB.prototype = new ClassA;
ClassB.prototype.b=function(){return "classbb"};

for (var l in ClassB.prototype){

    Array.prototype[l] = ClassB.prototype[l]; 
}
var array1 = [];
alert(array1.b()); 

Can

Array.prototype[l] = ClassB.prototype[l]

be replaced by

Array.prototype[l] = ClassB[l]

? Could someone help me? Thanks.

Upvotes: 1

Views: 88

Answers (1)

wong2
wong2

Reputation: 35720

No, you can't.ClassB has no property b, ClassB.prototype has.
If you do, in alert(array1.b()); array1.b will be undefined

Upvotes: 2

Related Questions