JQuery conflict with native prototype

I have a problem using jQuery with native JavaScript ( NOT prototype.js). When using the following jQuery 1.9.1 code with an error message:

Object.prototype.myVeryGreatFunction = function() { // ... } [Error] TypeError: undefined is not a function (evaluating 'U[a].exec(s)') ft (jquery.min.js, line 4) wt (jquery.min.js, line 4) st (jquery.min.js, line 4) find (jquery.min.js, line 4) init (jquery.min.js, line 3) b (jquery.min.js, line 3) (anonymous function) (read.control.js, line 59) c (jquery.min.js, line 3) fireWith (jquery.min.js, line 3) ready (jquery.min.js, line 3) H (jquery.min.js, line 3) 

When I remove the prototype definition, everything works fine. Unfortunately, I cannot easily upgrade jQuery because it is a plugin for CMS, so it should work with older versions for compatibility reasons.

Is there any known issue with this or a fix for this?

Googling actually shows me solutions like using jQuery.noConflict() and closing private functions. But, as mentioned above, I am not using prototype.js prototype, but native .

+6
javascript jquery prototype conflict
source share
2 answers

You can avoid these problems by making your extensions for your own prototypes non-enumerable:

 Object.defineProperty(Object.prototype, 'myVeryGreatFunction',{ value : function() {}, enumerable : false }); 

Object.defineProperty MDN documentation

As Jan DvoΕ™Γ‘k noted, this solution does not work for older browsers (IE8 -).

+10
source share

There is no safe way in older browsers to extend Object.prototype without breaking jQuery. JQuery authors specifically say (somewhere ...) that jQuery will break if you just add new enumerated properties to Object.prototype .

The only safe way requires ECMAScript 5 and its Object.defineProperty function, which allows you to add non-enumerable properties to objects.

The jQuery noconflict() function does not help - this is a solution to a completely different problem.

+2
source share

All Articles