jQuery 是否保证与数组或对象的扩展原型兼容?

发布于 2024-11-03 11:40:54 字数 309 浏览 0 评论 0原文

我想扩展Array和Object,如下代码:


$(window).load(function() {
    Array.prototype.x = 3
    Object.prototype.y = 4

    var a = [1, 2]
    for (var i in a) {
        alert(i + ': ' + a[i])
    }
})

/*
[result]
0: 1
1: 2
x: 3
y: 4
*/

这段代码会对当前版本或未来版本的jQuery中的jQuery操作造成任何麻烦吗?

I want to extend Array and Object like the following code:


$(window).load(function() {
    Array.prototype.x = 3
    Object.prototype.y = 4

    var a = [1, 2]
    for (var i in a) {
        alert(i + ': ' + a[i])
    }
})

/*
[result]
0: 1
1: 2
x: 3
y: 4
*/

Will this code cause any trouble to jQuery operation in current version or future version of jQuery?

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

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

发布评论

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

评论(3

唯憾梦倾城 2024-11-10 11:40:54

有了Array.prototype,我认为你非常安全。我不认为任何地方都有明确的保证,但 jQuery 多年来一直与 Prototype 和 MooTools 共存于网站上,并且它们都扩展了 Array.prototype。我认为 jQuery 团队不想破坏与这些库的兼容性。

但是:永远不要扩展Object.prototype。这很可能会破坏 jQuery 自己的代码,并且几乎肯定也会破坏插件代码。大多数 for..in 循环假定 {} 没有可枚举属性。如果您添加到 Object.prototype,您就会使该假设无效。 (除非您使用新的 ECMAScript5 defineProperty 功能,该功能允许您使属性不可枚举,但它尚未得到广泛支持。不过,为了好玩,你可以在这里尝试一下。)

With the Array.prototype, I think you're pretty safe. I don't think there's an explicit guarantee anywhere, but jQuery has been co-existing on sites with Prototype and MooTools for years, and they both extend the Array.prototype. I don't think the jQuery team would want to break compatibility with those libraries.

But: Never extend the Object.prototype. That may well break jQuery's own code and will almost certainly break plug-in code as well. Most for..in loops assume that {} will have no enumerable properties. If you add to Object.prototype, you're making that assumption invalid. (Unless you use the new ECMAScript5 defineProperty feature that lets you make properties non-enumerable, but it's not widely-supported yet. For fun, though, you can try it out here.)

最终幸福 2024-11-10 11:40:54

不,据我所知,jQuery 不提供此类保证。

jQuery 本身不扩展任何本机原型,因此除了对 DOM 节点进行一些有限的篡改之外,它几乎是自包含的。但是,当您像这样扩展 ArrayObject 时,您不能依赖其他人编写的 jQuery 插件来表现良好。许多其他库实际上确实扩展了Array,但是扩展Object会产生更严重的后果,因为它会影响每个对象。

它可以与 jQuery 的所有未来版本一起使用吗?猜猜看:)

No, there are no such guarantees from jQuery that I am aware of.

jQuery itself doesn't extend any native prototypes, so it is pretty much self contained besides some limited tampering of DOM nodes. However, you can't rely on jQuery plugins written by others to also behave nicely when you extend Array and Object like this. A lot of other libraries do actually extend Array, but extending an Object has more severe consequences since it affects every single object.

Will it work with all future versions of jQuery? Take a guess :)

甜中书 2024-11-10 11:40:54

John Resig(jQuery 的创建者)曾经写道,jQuery 可以安全地扩展 Array.prototype,但不能 100% 安全地扩展 Object.prototype(我不记得确切的位置,可能是在新的 jQuery 版本的公告中) 。 AFAIK 没有正式的保证,但 jQuery 的目标是抵抗此类更改和问题扩展原型(例如 thisthis) 被视为错误处理,尽管您可以从的例子,修复它们可能需要一段时间,因此即使扩展 Array,也会在一定程度上增加遇到错误的风险。

John Resig (creator of jQuery) once wrote that jQuery is safe against extending Array.prototype but not 100% safe against extending Object.prototype (I don't remember the exact place, it was probably in the announcement of a new jQuery version). There is no formal guarantee AFAIK, but it is an aim of jQuery to be resistant to such changes and problems extended prototypes (such as this or this) are handled as bugs, though as you can see from the examples, it can take a while to fix them, so even by extending Array you increase the risk of hitting a bug somewhat.

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