开发者

array inheritance question in javascript

开发者 https://www.devze.com 2023-02-19 12:52 出处:网络
function ClassA() { this.a=[]; this.aa=100; } function ClassB() {} ClassB.prototype = new Clas开发者_如何学JAVAsA;
function ClassA() { this.a=[]; this.aa=100; }
function ClassB() {  }

ClassB.prototype = new Clas开发者_如何学JAVAsA;
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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消