1

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 ?

5
  • 2
    Yes, Array .prototype.bubbleSort Commented Jun 10, 2016 at 16:00
  • 2
    Or myArray.constructor.prototype.bubbleSort = function(){};. Commented Jun 10, 2016 at 16:02
  • 1
    If you only want it on a single instance, just add it directly. myArray.bubbleSort = function() { ... };. Simple as that. Adding it to the prototype means it will be added to many instances. Commented Jun 10, 2016 at 16:03
  • @xofux Thank you. This works but can you please elaborate on why this works ? Does it have side effects ? Will all the other array methods work once i do this ? Commented Jun 10, 2016 at 16:05
  • @YasinYaqoobi Based on your update, juvian has the correct answer. Commented Jun 10, 2016 at 16:22

3 Answers 3

4

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();
Sign up to request clarification or add additional context in comments.

2 Comments

The "nicer way" doesn't entirely replicate an Array. For example, mutations of the .length property won't behave as you'd expect. Adding a function to the object doesn't actually break code using a for-in. If they're using that kind of loop, then they should expect to get all enumerable properties. If they only wanted indices, they'd use a for loop.
@squint you are right, first method should be preffered unless you have the risk of another library setting array.bubbleSort property
2

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();

Comments

1

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();

3 Comments

Thank you but I already know this. My question is about extending a single instance.
@YasinYaqoobi he means instead of using myArray.prototype.bubbleSort() the proper syntax is: Array.prototype.bubbleSort()
:) I understand the proper syntax but I purposefully wrote it that way to make sure it is obvious that I don't want to add to the main Array.prototype. I want to change just a single instance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.