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


function ClassB()  
{  
    this.b=function(){return "classbb"};  
}  
ClassB.prototype=new ClassA();  
Array.prototype= new ClassB();  
var array1= new Array();
alert(array1.b());

Why can't Array inherit ClassA and ClassB? Thanks.

1
  • I hope we both agree that this question is of theoretical nature, since Array would be rendered useless if you overwrote its prototype. Commented Mar 25, 2011 at 8:04

2 Answers 2

3

That's not quite the way to make the Array.prototype inherit from your objects. It would overwrite Array.prototype, which isn't allowed obviously.

You can however extend the prototype of Array with the properties/methods of ClassA/ClassB like this:

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

function ClassB() {  
  this.b=function(){return "classbb"};  
}

ClassB.prototype = new ClassA; 

var instB = new ClassB;
for (var l in instB){
    Array.prototype[l] = instB[l];
}

var array1 = [];
alert(array1.aa);

You can also:

Array.prototype.classb = new ClassB;
var array1 = [];
alert(array1.classb.aa);
Sign up to request clarification or add additional context in comments.

6 Comments

Can I write it this way? function ClassA() { this.a=[]; this.aa=100; } function ClassB() { this.b=function(){return "classbb"}; } ClassB.prototype = new ClassA; for (var l in ClassB.prototype){ Array.prototype[l] = ClassB.[l]; } var array1 = []; alert(array1.b);
Not really. First, you'll need to assign Classb.prototype[l] (which you are enumerating in a for loop). Second, if you do so, you will miss ClassB.b, because that's not in ClassB's prototype. It would work if you added method b to ClassB's prototype. See jsfiddle.net/pgw9q
Could you tell me why it still doesn't work if I write it this way: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){ // console.log(l); Array.prototype[l] = ClassB.[l]; //I delete prototype here } var array1 = []; alert(array1.b());
ClassB.[l] should be ClassB.prototype[l]. What do you mean with "I delete prototype here"? Nothing is deleted in your code.
Why can't ClassB.prototype[l] be replaced by ClassB[l]? I am new in javascript. Please forgive my ignorance.
|
1

The standard prohibits overwriting Array.prototype:

The initial value of Array.prototype is the Array prototype object (15.4.4).
This property has the attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }.

You can easily verify that browsers comply to this:

var origArrayProto = Array.prototype;
Array.prototype = new function () {}; // try to overwrite
alert(Array.prototype == origArrayProto); // true

Comments

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.