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