Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  12] [ 1]  / answers: 1 / hits: 16627  / 15 Years ago, tue, december 1, 2009, 12:00:00

I have added a simple .js file to my page that has some pretty mundane common-task sort of functions added to the Object and Array prototypes.



Through trial and error, I've figured out that adding any function to Object.prototype, no matter it's name or what it does causes Javascript errors in jQuery:



The culprit?



Object.prototype.foo = function() {
/*do nothing and break jQuery*/
};


The error I'm getting line 1056 of jquery-1.3.2.js, in the attr:function { } declaration:



/*Object doesn't support this property or method*/
name = name.replace(/-([a-z])/ig, function(all, letter) {
return letter.toUpperCase();
});


Apparently G.replace is undefined.



While it's obvious that there's something I'm just not wrapping my head around with prototyping, I'm failing miserably to figure out what it is.



To be clear, I'm not looking for a workaround, I have that handled... what I'm looking for is an answer to Why?. Why does adding a function to Object.prototype break this bit of code?


More From » jquery

 Answers
47

You should never extend Object.prototype. It does far more than break jQuery; it completely breaks the object-as-hashtables feature of Javascript. Don't do it.



You can ask John Resig, and he'll tell you the same thing.


[#98185] Thursday, November 26, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
theodore

Total Points: 318
Total Questions: 97
Total Answers: 119

Location: Turks and Caicos Islands
Member since Sun, Mar 7, 2021
3 Years ago
;