将自定义函数添加到 Array.prototype [英] adding custom functions into Array.prototype

查看:50
本文介绍了将自定义函数添加到 Array.prototype的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个支持 AJAX 的 asp.net 应用程序.我刚刚向 Array.prototype 添加了一些方法,例如

I was working on an AJAX-enabled asp.net application. I've just added some methods to Array.prototype like

Array.prototype.doSomething = function(){
   ...
}

这个解决方案对我有用,可以以漂亮"的方式重用代码.

This solution worked for me, being possible reuse code in a 'pretty' way.

但是当我在整个页面上测试它时,我遇到了问题..我们有一些自定义的 ajax 扩展器,它们开始表现得出乎意料:一些控件在其内容或值周围显示未定义".

But when I've tested it working with the entire page, I had problems.. We had some custom ajax extenders, and they started to behave as the unexpected: some controls displayed 'undefined' around its content or value.

这可能是什么原因?我是否缺少有关修改标准对象原型的内容?

What could be the cause for that? Am I missing something about modifing the prototype of standart objects?

注意:我很确定错误是在我修改 Array 的原型时开始的.它应该只与 IE 兼容.

Note: I'm pretty sure that the error begins when I modify the prototype for Array. It should be only compatible with IE.

推荐答案

修改内置对象原型通常是一个坏主意,因为它总是有可能与来自其他供应商的代码或加载到同一页.

Modifying the built-in object prototypes is a bad idea in general, because it always has the potential to clash with code from other vendors or libraries that loads on the same page.

在 Array 对象原型的情况下,这是一个特别糟糕的主意,因为它有可能干扰迭代任何数组成员的任何代码段,例如 for .. in.

In the case of the Array object prototype, it is an especially bad idea, because it has the potential to interfere with any piece of code that iterates over the members of any array, for instance with for .. in.

用一个例子来说明(从这里借来):

To illustrate using an example (borrowed from here):

Array.prototype.foo = 1;

// somewhere deep in other javascript code...
var a = [1,2,3,4,5];
for (x in a){
    // Now foo is a part of EVERY array and 
    // will show up here as a value of 'x'
}

不幸的是,有问题的代码的存在使得有必要避免使用普通的 for..in 进行数组迭代,至少如果你想要最大的可移植性,只是为了防止出现情况其他一些讨厌的代码修改了 Array 原型.所以你真的需要两者都做:你应该避免简单的 for..in 以防某些 n00b 修改了 Array 原型,并且你应该避免修改 Array 原型,这样你就不会弄乱任何代码使用普通的 for..in 来遍历数组.

Unfortunately, the existence of questionable code that does this has made it necessary to also avoid using plain for..in for array iteration, at least if you want maximum portability, just to guard against cases where some other nuisance code has modified the Array prototype. So you really need to do both: you should avoid plain for..in in case some n00b has modified the Array prototype, and you should avoid modifying the Array prototype so you don't mess up any code that uses plain for..in to iterate over arrays.

您最好创建自己类型的对象构造函数并带有 doSomething 函数,而不是扩展内置数组.

It would be better for you to create your own type of object constructor complete with doSomething function, rather than extending the built-in Array.

Object.defineProperty 怎么样?

What about Object.defineProperty?

现在存在 Object.defineProperty 作为扩展对象原型的一般方法,而新属性是不可枚举的,尽管这仍然不能证明扩展内置类型,因为即使除了 for..in 之外,仍然有可能与其他脚本发生冲突.考虑有人使用两个 Javascript 框架,它们都尝试以类似的方式扩展 Array 并选择相同的方法名称.或者,考虑有人分叉您的代码,然后将原始版本和分叉版本放在同一页面上.Array 对象的自定义增强功能仍然有效吗?

There now exists Object.defineProperty as a general way of extending object prototypes without the new properties being enumerable, though this still doesn't justify extending the built-in types, because even besides for..in there is still the potential for conflicts with other scripts. Consider someone using two Javascript frameworks that both try to extend the Array in a similar way and pick the same method name. Or, consider someone forking your code and then putting both the original and forked versions on the same page. Will the custom enhancements to the Array object still work?

这就是 Javascript 的现实,以及为什么您应该避免修改内置类型的原型,即使使用 Object.defineProperty.使用您自己的构造函数定义您自己的类型.

This is the reality with Javascript, and why you should avoid modifying the prototypes of built-in types, even with Object.defineProperty. Define your own types with your own constructors.

这篇关于将自定义函数添加到 Array.prototype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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