Javascript中的Prototyping对象打破了jQuery? [英] Prototyping Object in Javascript breaks jQuery?

查看:145
本文介绍了Javascript中的Prototyping对象打破了jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的页面中添加了一个简单的 .js 文件,其中添加了一些非常普通的常见任务类型的函数 Object 数组原型。

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.

通过反复试验,我发现添加任何函数到 Object.prototype ,无论它的名称或它的作用导致jQuery中的Javascript错误:

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:

罪魁祸首?

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

错误我得到 jquery-1.3.2的第1056行。 js ,在attr:function {}声明中:

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();
        });

显然G.replace未定义。

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.

要清楚,我不是在寻找一种解决方法,我已经处理好了......我正在寻找的是为什么?的答案。为什么在 Object.prototype 中添加一个函数会破坏这段代码?

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?

推荐答案

你永远不应该扩展 Object.prototype 。它不仅仅是打破jQuery;它彻底打破了Javascript的object-as-hashtables功能。不要这样做。

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.

你可以问John Resig,他会告诉你同样的事情

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

这篇关于Javascript中的Prototyping对象打破了jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆