Yasin Yaqoobi
Yasin Yaqoobi

Reputation: 2050

Can I add javascript Prototype to my array instance?

So I understand that you can add new methods to your javascript object prototype. myObject.prototype.myFunction = function {}. I wanted to do the same for my array instance but keep getting errors. Something like this.

var myArray = [1,2,3,4,5,6,7,8,9,0]; 

myArray.prototype.bubbleSort = function(){

// sort stuff here. 

}

Why is this not allowed ? I thought since arrays inherit from Object you should be able to extend them the same way as objects.

If this is not possible how can I add a . notation function call to my arrays so I can say. myArray.bubbleSort(); instead of bubbleSort(myArray); without adding methods to Array.prototype.


I think my question caused some confusion so please read this section before answering. Thank you.

My goal is to have an array named myArray that has a specific method bubbleSort. I don't want to change the main Array.Prototype.

So later I can do something like this. var yourArray = new myArray() so now since yourArray is instance of myArray it will have access to bubblesort function.

Can we achieve this without changing the main Array.prototype ?

Upvotes: 1

Views: 2164

Answers (3)

juvian
juvian

Reputation: 16068

Arrays are also objects, so you can easily add it as a function to only that array:

var myArray = [1,2,3,4,5,6,7,8,9,0]; 
myArray.bubbleSort  = function(){console.log(this)}

Beware that if you do this in a library, you might break code like this:

for(var i in myArray){
    console.log(i)
}

Because that will also give the bubbleSort property

A nicer way would be to extend an Array to make a wrapper:

function sortableArray(){
    return this
}

sortableArray.prototype = Object.create(Array.prototype);

sortableArray.prototype.bubbleSort = function(){
  console.log(this)
}

var arr = new sortableArray()
arr.push(1)
arr.push(0)
arr.push(9)
arr.bubbleSort();

Upvotes: 4

Mike Cluck
Mike Cluck

Reputation: 32511

I think you're misunderstanding what the prototype is for. The prototype provides a chain for objects to link other properties and methods to a given object.

For example, when you create an array

[]

that array has access to various functions like forEach, reduce, etc. because they are linked to, or inherited through, the prototype. Modifying that prototype will modify all objects which inherit from the same prototype.

var aNewArray = [6, 7, 8];
var myArray = [1, 2, 3, 4, 5];

// myArray.constructor.prototype will give you access to myArrays prototype
myArray.constructor.prototype.forMyArrayOnly = function() {
  console.log(this === myArray ? 'this is myArray' : 'this is NOT myArray!');
};

myArray.forMyArrayOnly();
aNewArray.forMyArrayOnly();

If you just want to add a method or property to a single object, you don't want to modify their prototype chain. Instead, you want to add that property directly to the object.

var myArray = [1, 2, 3, 4, 5];
myArray.bubbleSort = function() {
  console.log('I totally just did bubble sort. Check it out');
  console.log(this);
};

myArray.bubbleSort();

Upvotes: 2

Jamiec
Jamiec

Reputation: 136134

You should have used Array.prototype.bubbleSort

var myArray = [1,2,3,4,5,6,7,8,9,0]; 

Array.prototype.bubbleSort = function(){

    console.log("Do bubble sort here");

}

myArray.bubbleSort();

Upvotes: 1

Related Questions