adding custom functions into Array.prototype

arrays, internet-explorer, javascript

Solution

While the potential for clashing with other bits o' code the override a function on a prototype is still a risk, if you want to do this with modern versions of JavaScript, you can use the `Object.defineProperty` method, e.g.

// functional sort
Object.defineProperty(Array.prototype, 'sortf', {
    value: function(compare) { return [].concat(this).sort(compare); }
});

Problem

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.

Original source

Related problems