Prototyping Object in Javascript breaks jQuery?

Posted by blesh on Stack Overflow See other posts from Stack Overflow or by blesh
Published on 2009-12-01T16:49:36Z Indexed on 2010/03/13 7:55 UTC
Read the original article Hit count: 500

I have added a simple .js 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?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about prototype