问题描述
我在使用jQuery和原生JavaScript( NOT prototype.js)时遇到问题。当使用以下代码时,jQuery 1.9.1带有错误消息:
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:
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)
当我删除原型定义时,一切都很有效。不幸的是,我无法轻松更新jQuery,因为这是一个CMS的插件,因此出于兼容性原因,它必须使用旧版本。
When I remove the prototype definition, everything works great. Unfortunately I can't easily update jQuery because this is in a plugin for a CMS, so it has to work with old versions for compatibility reasons.
是否有任何已知问题用那个或修复它?
Is there any known issue with that or a fix for that?
谷歌搜索实际上向我展示了使用 jQuery.noConflict()
和私有的解决方案功能包装。但如上所述,我没有使用prototype.js,而是原生 JS对象原型。
Googling actually shows me solutions like using jQuery.noConflict()
and private function wrapping. But as mentioned above I'm not using prototype.js, but native JS object prototyping.
推荐答案
您可以通过将本机原型扩展为不可枚举来避免这些问题:
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
正如Jan Dvorak所说,这个解决方案不适用于旧浏览器(IE8 - )。
As Jan Dvorak mentioned, this solution does not work for old browsers (IE8-).
这篇关于jQuery与本机原型冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!