开发者

array inheritance question in javascript

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜