jQuery + extends Object.prototype = "c.replace 不是一个函数"

发布于 2024-10-18 12:32:20 字数 905 浏览 1 评论 0 原文

我在我的开源项目中使用 jQuery 1.5,并且以下行也出现在我自己的 Javascript 代码中:

/**
 * Object.isEmpty()
 *
 * @returns {Boolean}
 */
Object.prototype.isEmpty = function ()
{
    /**
     * @deprecated Since Javascript 1.8.5
     * @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object
     */
    if ( this.__count__ !== undefined )
    {
        return this.__count__ === 0 ? true : false;
    }

    /* Less-aesthetic method, if above method fails */
    for ( var property in this )
    {
        if ( this.hasOwnProperty(property) )
        {
            return false;
        }
    }
    return true;
};

它只是扩展了 Object.prototype,向其添加 isEmpty() 方法 [检查对象是否为空]。由于此添加,我在 Firebug 控制台中收到“c.replace 不是函数”错误;我在网络上的研究引导我找到了 jQuery bug 跟踪器消息,在那里我“学会”了扩展Object.prototype 不仅破坏了 jQuery,而且也是不好的编码习惯。我的问题是,为什么?

I am using jQuery 1.5 in my open source project and following line is also present in my own Javascript code:

/**
 * Object.isEmpty()
 *
 * @returns {Boolean}
 */
Object.prototype.isEmpty = function ()
{
    /**
     * @deprecated Since Javascript 1.8.5
     * @see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object
     */
    if ( this.__count__ !== undefined )
    {
        return this.__count__ === 0 ? true : false;
    }

    /* Less-aesthetic method, if above method fails */
    for ( var property in this )
    {
        if ( this.hasOwnProperty(property) )
        {
            return false;
        }
    }
    return true;
};

which just extends Object.prototype adding isEmpty() method to it [that checks whether the object is empty or not). Because of this addition, I am getting "c.replace is not a function" error in my Firebug console; and my research on the web lead me to jQuery bug tracker message, where I "learned" that extending Object.prototype not only breaks jQuery, but also is bad coding practice. My question is, why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

纵情客 2024-10-25 12:32:20

ECMA-262 第五版(和 JavaScript 1.8.5)可以通过 Object.definePropertyObject.defineProperties 方法,通过设置 enumerable< /code> 属性字段设置为 false。 Chrome 5、Safari 5、Firefox 4 和 Internet Explorer 9 或任何最近使用 V8 的服务器端实现(如 Node.js)中都提供了该功能。

ECMA-262 5th Edition (and JavaScript 1.8.5) has ways to do it through the Object.defineProperty and Object.defineProperties methods, by setting the enumerable field of the property to false. That is available in Chrome 5, Safari 5, Firefox 4 and Internet Explorer 9 or any recent server side implementation that uses V8 (like Node.js).

树深时见影 2024-10-25 12:32:20

基本上,这是因为扩展 Object.prototype 破坏了 for ... in 习惯用法。

在 Javascript 中,如果您有一个对象:

var obj = { "foo": 0, "bar": 42 };

您可以通过执行以下操作来迭代其成员:

for (var key in obj) {
    // Do Something.
}

扩展 Object.prototype 将导致扩展成员出现在所有对象实例中,因此上面的代码将迭代更多键比 foobar 更可能会产生意想不到的结果。

您可以在此处找到演示该问题的小提琴。

Basically, that because extending Object.prototype breaks the for ... in idiom.

In Javascript, if you have an object:

var obj = { "foo": 0, "bar": 42 };

You can iterate over its members by doing:

for (var key in obj) {
    // Do Something.
}

Extending Object.prototype will result in the extended members being present in all object instances, so the code above would iterate over more keys than foo and bar, with probably unexpected results.

You can find a fiddle demonstrating the problem here.

将军与妓 2024-10-25 12:32:20

1) 如何向 Object 添加(扩展)附加方法(而不是属性)?

只要第三方代码在您的页面上运行,您就不应该运行。

2) 如果您可以在 hasOwnProperty() 的帮助下区分对象的直接子对象和全局对象,为什么这是一个糟糕的编码?

因为其他程序员很可能很懒,而您破坏了他们的代码。最好不要修改不属于您的东西Object.prototype 就是其中之一。

在您自己的范围内使用 MyLib.isEmpty(obj)isEmpty(obj) 这样就不会发生冲突。

1) How to add (extend) additional methods (not properties) to Object?

As long as third party code is running on your page you shouldn't.

2) If you can distinguish immediate children of your object from global ones, with help of hasOwnProperty(), why it is a bad coding?

Because there's a likelihood that other programmers are lazy, and you break their code. It is a good practice not to modify what you don't own. Object.prototype is one of these things.

Use MyLib.isEmpty(obj), or isEmpty(obj) inside your own scope so there's no chance to collide.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文