1

Possible Duplicate:
Is it an anti-pattern to modify JavaScript's built-in prototypes?

I just learned that it's possible to modify the JavaScript core objects, but the same article that told me how to do it suggested that I never should.

Is it a bad thing to modify a core JS object? If so, what problems could this cause?

As an example, here's a modification of the Array object that gives me an easy way to search for a given value in an array:

Array.prototype.search = function(val){
    var i;
    for (i = 0; i < this.length; i++){
        if (this[i] == val) {
            console.log("found: " + val);
            return true;
        }
    }
    console.log("didn't find it");
    return false;
};

Here's the article: Advanced Javascript: Objects, Arrays, and Array-Like objects

Community
  • 1
  • 1
  • 2
    Related: http://stackoverflow.com/questions/1676383/is-it-an-anti-pattern-to-modify-javascripts-built-in-prototypes, http://stackoverflow.com/questions/6023409/why-is-extending-the-dom-built-in-object-prototypes-a-bad-idea, http://stackoverflow.com/questions/6223449/why-is-it-frowned-upon-to-modify-javascript-objects-prototypes, possibly more – Digital Plane Aug 21 '11 at 08:11

5 Answers5

4

Yes, when modifying so-called host objects the behaviour is undefined. It might work (usually it does for objects like Array, String and Object), it might break things.

However, you should never modify the prototype of Array or Object as it will break for(var key in element) for arrays and objects unless you include if(!element.hasOwnProperty(key)) continue; at the beginning of all those loops. So never do it even if you don't use for..in loops.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • 4
    `Array`, `String`, and `Object` are not host objects, they're [native objects](http://es5.github.com/#x4.3.6). (`window` and `document` are examples of [host objects](http://es5.github.com/#x4.3.8).) Re breaking `for..in` loops if you modify `Array.prototype`, with respect, any `for..in` loop that expects to loop only across the elements of the array [is *already* broken](http://blog.niftysnippets.org/2010/11/myths-and-realities-of-forin.html). Completely agreed about modifying `Object.prototype`, though, that's an absolute no-no. – T.J. Crowder Aug 21 '11 at 08:28
2

Whether is bad or not is quite debatable, I really like the point of view that @kangax gives us in this article:

At first he separates the extension of host and native objects, with host objects there are no guarantees, there's no a spec, and they have no rules.

Host objects are those that are provided by the environment, they are not part of the ECMAScript specification, for example DOM objects.

Extending native objects seems "less dangerous", he explores the problems of enumerability, which seems to be a problem, the for-in statement enumerates all object properties, inherited or own, if you add a property on Object.prototype for example, any object used with this statement will show the property name.

This statement is often abused to iterate over array objects, while it's purpose is to enumerate object properties.

At the end, I think the conclusion of this article is that extending native objects to shim standard compliant methods (e.g. ES5 Array methods (map, forEach, reduce, etc)) is encouraged, but extending natives to add custom non-standard compliant methods is discouraged.

SavoryBytes
  • 35,571
  • 4
  • 52
  • 61
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
1

The problems usually come from wrong behavior in other libraries or code that don't expect those extra properties.

What you should never touch is Object.prototype as that has a very high chances of breaking any for .. in loop, but an array is usually looped on the items, so it's less risky.

BTW, what you are doing is available in some browsers (at least Firefox) as Array.prototype.indexOf

AlfonsoML
  • 12,634
  • 2
  • 46
  • 53
0

This will not necessarily cause problems, but if you are working in a production environment it is not recommended. All around, it is general practice to not modify objects that you did not create; take a look at http://www.nczonline.net/blog/2010/03/02/maintainable-javascript-dont-modify-objects-you-down-own/

Dave Lasley
  • 5,262
  • 1
  • 34
  • 37
0

For one, someone using maintaining code might freak out seeing that some code does not behave the way she expects.

Nivas
  • 18,126
  • 4
  • 62
  • 76