jQuery conflict with native prototype

后端 未结 2 1042
萌比男神i
萌比男神i 2021-01-05 15:55

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

         


        
2条回答
  •  囚心锁ツ
    2021-01-05 16:22

    You can avoid these problems by making your extensions to the native prototypes as non-enumerable:

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

    Object.defineProperty documentation on MDN

    As Jan Dvorak mentioned, this solution does not work for old browsers (IE8-).

提交回复
热议问题