1
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.

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

6 Comments

@wong2. If I want to define ClassB.b, how should I do? Thanks
@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"};
@wong2: Could you tell me the difference between ClassB.b and ClassB.prototype.b? Thank you very much. And what does ClassB.property mean?
@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?
@wong2: function ClassB() { this.b = function(){return "classbb"}; } ClassB.b = function(){return "classBB"}; What is the difference between this.b and ClassB.b?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.