0

How can I manipulate the prototype of a predefined object (for example an Array) so that it does something when creating instances of that object?

Simply I want to alert('an array was created!') whenever an Array is instantiated.

3
  • That's tricky, given that you can create an array using [123] as well. Not sure if it's possible at all. Commented Aug 30, 2012 at 20:54
  • It is considered to be a bad practice - to modify the built-in types. Commented Aug 30, 2012 at 20:55
  • You might be able to replace window.Array with a completely different constructor of your own, which then proceeds to instantiate an actual Array (which you would need to have saved somewhere, since you don't have it anymore at window.Array) and return it. Still, not really advisable. Commented Aug 30, 2012 at 20:57

3 Answers 3

3

You can set a new method on an array by adding it to the Array.prototype object:

Array.prototype.fizz = function () {
    alert('works');
};

var arr = [];
arr.fizz();

However, this only allows you to create new methods, this does not allow you to extend existing methods*, nor does it allow you to override the Array constructor.

Be careful adding new methods to existing types. This can adversely affect the entire scripting environment, and cause unexpected behavior in some libraries. Although some might call it "bad practice", it's quite common to use polyfills for cross-browser compatibility, such as by creating Array.prototype.indexOf.

There is no such thing as a "newed" array, the word is "instantiated":

var a1, a2;
a1 = []; //a1 was instantiated with a new Array
a2 = new Array(); //a2 was also instantiated with a new Array

There is no cross-browser means of overriding the Array constructor.

* it's possible to wrap an existing method in a function so that, when called, the existing method performs its original functionality, in addition to the new functionality. Although this might be referred to as extending an existing method, it is in fact creating a new method.

Sign up to request clarification or add additional context in comments.

Comments

2

You can try to override Array with your own function. It seems to work when doing new Array, but not when doing [].

(function() {
    var _ac = Array;
    Array = function() {
        alert('an array was newed!');
        return _ac.apply(this, arguments);
    };
}());

DEMO: http://jsfiddle.net/DAg9A/

2 Comments

Specs: [] uses Array, "where Array is the standard built-in constructor with that name.".
I never said you should do this, but you can :-)
1

I would suggest the you just create an array namespace for it:

array = {};
array.create = function() {

    alert("Created an array");     
    return [];

}

So whenever you create an array you use: array.create(); .

You should not, and in this case can not, change native functionality. You have to be in charge of every array creation.

Comments

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.