Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have created a JavaScript application that has a lot of array manipulations (sorting, filtering, etc.).

Currently my functions are like this:

function (myArray, arg1, arg2,...)

where myArray is the array I am working on, and arg1, arg2,... are the arguments used by the function to modify the array.

I am thinking that it would be neater to have a clear separation between the object and the arguments (a la jQuery):

myArray.function(arg1, arg2,...)

I know that I could use Array.prototype to add my function to all arrays, but this seems too heavy as the functions I add are really specific to my case and don't make sense on arrays in general. I also know that I could create an object, but then I wouldn't benefit from the array methods available in JavaScript (indexOf, filter, etc.).

Is there a way I could create my own array object, that would inherit the default array methods and allow me to add my own?

share|improve this question
3  
keep using the function, you don't need everything in oop –  dynamic Feb 10 '12 at 19:27
    
Right, but I like how clean it is, for example for chaining or defining default options/arguments. –  Christophe Feb 10 '12 at 19:31

4 Answers 4

up vote 5 down vote accepted

You've got two options:

Option one is to extend the Array object by adding new methods to it:

Array.prototype.myMethod = function(arg1, arg2)
{
    // Use 'this' to access the array.
}

The advantage is that every javascript array will now have this extra method. This can also be a disadvantage when multiple (external) javascript libraries try to add the same methods. Furthermore, this new method will appear in for(var item in myArray) constructs which can be problematic.

Option two is to create a "factory method" for your extended array:

function MyExtendedArray = function()
{
    var array = [];
    array.myMethod = function(arg1, arg2) {
        // Use 'this' or 'array' to access the array.
    };

    return array;
};

Now you can write this to create a new extended array:

var arr = MyExtendedArray();
share|improve this answer
    
I think the factory method is exactly what I need for my specific application. It's so obvious...not that I see it :-) Thanks! –  Christophe Feb 10 '12 at 20:00

No. Array cannot be subclassed and still behave like an array. Specifically, the length property will not change to reflect elements that are added or deleted from the array.

function MyArray() {}
MyArray.prototype = [];
MyArray.prototype.constructor = MyArray;
var instance = new MyArray;
instance[0] = 0;
alert(instance.length);  // alerts 0

The reason this happens is because only objects that are created via new Array or syntactic sugar like [] have the special handling around length.

The new MyArray above creates a regular object whose prototype is an Array, but there is nothing about the new operator that checks whether the prototype of an object is an Array and sets up the special length handling.

share|improve this answer
    
+1 thx for the explanations. –  Christophe Feb 10 '12 at 20:04

You can simply just add the function directly to your object rather than to the prototype:

var myArray = new Array();
myArray.myFunc = function(){ /* do stuff! */ };
myArray.myFunc();
share|improve this answer
    
+1 thx. I chose Elian's answer that makes it more generic. –  Christophe Feb 10 '12 at 20:02

Yes, just extend the Array prototype with jQuery.extend

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.