views:

376

answers:

3

I was working on an AJAX-enabled asp.net application. I've just added some methods to Array.prototype like

Array.prototype.doSomething = function(){
                                         ...
                                          }

This solution worked for me, being possible reuse code in a 'pretty' way.

But when I've tested it working with the entire page, I had problems.. We had some custom ajax extenders, and they started to behave as the unexpected: some controls displayed 'undefined' around its content or value.

What could be the cause for that? Am I missing something about modifing the prototype of standart objects?

Note: I'm pretty sure that the error begins when I modify the prototype for Array. It should be only compatible with IE.

+1  A: 

Modifying the built-in object prototypes can be a bad idea in general, because it always has the potential to clash with other code on the same page.

In the case of the Array object prototype, it is an especially bad idea, because it has the potential to interfere with any piece of code that iterates over the members of any array, for instance with for .. in.

To illustrate using an example (borrowed from here):

Array.prototype.foo = 1;

// somewhere deep in other javascript code...
var a = [1,2,3,4,5];
for (x in a){
    // Now foo is a part of EVERY array and 
    // will show up here as a value of 'x'
}

It would be better for you to create your own type of object constructor complete with doSomething function, rather than extending the built-in Array.

thomasrutter
I believe the "for (x in y)" construct is for iterating over the members of an object. For indexed-based iteration of an array, I don't think it's suitable. However, your point about interfering with other code on the page is valid - particularly if 3rd-party libraries are using for-in in this way.
harto
Yes, the inverse is true - you should avoid for..in in case some n00b has modified the Array prototype, and you should avoid modifying the Array prototype in case some n00b has used for..in on an array. ;)
thomasrutter
A: 

In general messing with the core javascript objects is a bad idea. You never know what any third party libraries might be expecting and changing the core objects in javascript changes them for everything.

If you use Prototype it's especially bad because prototype messes with the global scope as well and it's hard to tell if you are going to collide or not. Actually modifying core parts of any language is usually a bad idea even in javascript.

(lisp might be the small exception there)

Jeremy Wall
A: 

You augmented generic types so to speak. You've probably overwritten some other lib's functionality and that's why it stopped working.

Suppose that some lib you're using extends Array with function Array.remove(). After the lib has loaded, you also add remove() to Array's prototype but with your own functionality. When lib will call your function it will probably work in a different way as expected and break it's execution... That's what's happening here.

Robert Koritnik