How to make an inherited "class" from an array ( Array )?

The standard approach is:

 ArrayExt = function (){ Array.apply( this, arguments ); } tmp = function(){}; tmp.prototype = Array.prototype; ArrayExt.prototype = new tmp(); ArrayExt.prototype.constructor = ArrayExt; 

It does not work to the end ...

 test = new ArrayExt(); test[5] = 1; test.length;// == 0, а должно быть 6 

PS:

Creating a real array, with the replacement of its prototype:

 ArrayExt = function (){ var arr = []; arr.__proto__ = ArrayExt.prototype; return arr; } tmp = function(){}; tmp.prototype = Array.prototype; ArrayExt.prototype = new tmp(); ArrayExt.prototype.constructor = ArrayExt; 

Not an option (since strictly speaking, __proto__ should be an inaccessible property), although it works

 test = new ArrayExt(); test[5] = 1; test.length;// == 6 

PPS:

 Array.prototype.newMethod = function(){} 

Generally not an option (because correcting embedded objects is not gud).

  • Look at it , you can rewind a large half, as I understand it, you should have no problems, and then there goes the question of inheritance. Frankly, now, it seems that you, so far, have not understood what is happening and why. Well, there is a smart example of class imitation, very simple and cunning =) - Zowie
  • And by the way - who's stopping you to check for the existence of a particular property or method of the prototype? Here I’ll enter: Array.prototype.newMethod = function () {} And you don’t leave the rewriting anyway, somehow you don’t understand this moment ... - Zowie 4:21 pm

2 answers 2

So is also not an option?

 function MyArray() { this.__proto__ = Array.prototype; } function test1() { var t = new MyArray(); t.push(3); alert(t.pop()); } 

What's wrong with the code:

 test = new ArrayExt(); test[5] = 1; test.length;// == 6 

It seems that's right.


 function extend(Child, Parent) { var F = function() { } F.prototype = Parent.prototype Child.prototype = new F() Child.prototype.constructor = Child Child.superclass = Parent.prototype } function MyArray() { } extend(MyArray, Array); MyArray.prototype.myMethod = function(){ alert("Test Method"); } function test1() { var t = new MyArray(); t.push(1); t.push(2); alert("Length: " + t.length); alert("Pop: " + t.pop()); t.myMethod(); } 

So see the "right" will be.

  • 1) This is: function MyArray () {this .__ proto__ = Array.prototype; } Does not work, because: MyArray.prototype.myMethod = funciton () {}; test = new MyArray (); test.myMethod; // undefined 2) "test.length; // == 6" is not an option, since manual replacement of the prototype of the object - not gud - timka_s
  • You tell me what you want to end up with. - Alex Kapustin
  • Make the "class", whose parent is Array, the most correct in terms of the specification of the language - timka_s
  • updated answer. - Alex Kapustin
  • Ваш код == Мой первый код == Стандартный подход , and it is NOT true ... Normal array: t = []; t [5] = 10; t.length; // 6 Your code: t = new MyArray (); t [5] = 10; t.length; // 0 - timka_s

So it seems with the use of prototype inheritance nothing else.

  • KO in action ... This is not a theoretical question, but a practical one - how to achieve the necessary result (code?) - timka_s