i try to extend Array object in javascript with some user friendly methods like Array.Add() instead Array.push() etc...
i implement 3 ways to do this. unfortunetly the 3rd way is not working and i want to ask why? and how to do it work.
//------------- 1st way
Array.prototype.Add=function(element){
this.push(element);
};
var list1 = new Array();
list1.Add("Hello world");
alert(list1[0]);
//------------- 2nd way
function Array2 () {
//some other properties and methods
};
Array2.prototype = new Array;
Array2.prototype.Add = function(element){
this.push(element);
};
var list2 = new Array2;
list2.Add(123);
alert(list2[0]);
//------------- 3rd way
function Array3 () {
this.prototype = new Array;
this.Add = function(element){
this.push(element);
};
};
var list3 = new Array3;
list3.Add(456); //push is not a function
alert(list3[0]); // undefined
in 3rd way i want to extend the Array object internally Array3 class. How to do this so not to get "push is not a function" and "undefined"?
Here i add a 4th way.
//------------- 4th way
function Array4 () {
//some other properties and methods
this.Add = function(element){
this.push(element);
};
};
Array4.prototype = new Array();
var list4 = new Array4();
list4.Add(789);
alert(list4[0]);
Here again i have to use prototype. I hoped to avoid to use extra lines outside class constructor as Array4.prototype. I wanted to have a compact defined class with all pieces in one place. But i think i cant do it otherwise.
Method names should be lowercase. Prototype should not be modified in the constructor.
in CoffeeScript
If you don't like that syntax, and you HAVE to extend it from within the constructor, Your only option is:
You could also do this
In olden days, 'prototype.js' used to have a Class.create method. You could wrap all this is a method like that
For more info on this and how to implement, look in the prototype.js source code
A while ago I read the book Javascript Ninja written by John Resig, the creator of jQuery. He proposed a way to mimic array-like methods with a plain JS object. Basically, only
length
is required.I don't mean it's good or bad. It's an original way of doing
Array
operations. The benefit is that you do not extend theArray prototype
. Keep in mind thatobj
is a plainobject
, it's not anArray
. Thereforeobj instanceof Array
will returnfalse
. Thinkobj
as a façade.If that code is of interest to you, read the excerpt Listing 4.10 Simulating array-like methods.
The answer is a compact workaround which works as intended in all supporting browsers.
In your third example you're just creating a new property named
prototype
for the objectArray3
. When you donew Array3
which should benew Array3()
, you're instantiating that object into variablelist3
. Therefore, theAdd
method won't work becausethis
, which is the object in question, doesn't have a valid methodpush
. Hope you understand.Edit: Check out Understanding JavaScript Context to learn more about
this
.Are you trying to do something more complicated then just add an alias for "push" called "Add"?
If not, it would probably be best to avoid doing this. The reason I suggest this is a bad idea is that because Array is a builtin javascript type, modifying it will cause all scripts Array type to have your new "Add" method. The potential for name clashes with another third party are high and could cause the third party script to lose its method in favour of your one.
My general rule is to make a helper function to work on the Array's if it doesnt exist somewhere already and only extend Array if its extremely necessary.
ES6
Using
__proto__
(old answer, not recommended, may cause performance issues)
Now you can add your methods to
SubArray
Initialize like normal Arrays
Behaves like normal Arrays