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.
asked Mar 25, 2011 at 12:07
jsnewman
3391 gold badge2 silver badges8 bronze badges
1 Answer 1
No, you can't.ClassB has no property b, ClassB.prototype has.
If you do, in alert(array1.b()); array1.b will be undefined
answered Mar 25, 2011 at 12:11
wong2
36.1k51 gold badges138 silver badges182 bronze badges
Sign up to request clarification or add additional context in comments.
6 Comments
jsnewman
@wong2. If I want to define ClassB.b, how should I do? Thanks
wong2
@jsnewman I'm not sure if I understand what you want to do...You can define ClassB.b by
function ClassB() {}; ClassB.b = function(){return "classbb"};jsnewman
@wong2: Could you tell me the difference between ClassB.b and ClassB.prototype.b? Thank you very much. And what does ClassB.property mean?
wong2
@jsnewman If you define
ClassB.prototype.b to some value, then every instance created by new ClassB();(like var class_b = new ClassB();) will have a property b, but if you define ClassB.b, then b is just a property of ClassB(who is a function, and also an object).Is that clear?jsnewman
@wong2: function ClassB() { this.b = function(){return "classbb"}; } ClassB.b = function(){return "classBB"}; What is the difference between this.b and ClassB.b?
|
lang-js