Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  9] [ 7]  / answers: 1 / hits: 92406  / 15 Years ago, thu, june 4, 2009, 12:00:00

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.


More From » arrays

 Answers
8

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

[#99390] Saturday, May 30, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tobyl

Total Points: 598
Total Questions: 110
Total Answers: 114

Location: Vietnam
Member since Sat, Feb 12, 2022
2 Years ago
tobyl questions
Tue, Aug 10, 21, 00:00, 3 Years ago
Wed, Jan 13, 21, 00:00, 3 Years ago
Tue, Dec 1, 20, 00:00, 4 Years ago
;