对于Javascript,什么阻止现代实现不将参数视为真正的数组? [英] For Javascript, what prevents modern implementation not to treat arguments as a real array?

查看:100
本文介绍了对于Javascript,什么阻止现代实现不将参数视为真正的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这是一个旧的Javascript行为(Crockford说这是一个设计错误),在函数内部, arguments 就像一个数组,除了它不是一个真实的数组,因此无法在其上调用数组方法:

I think it is an old Javascript behavior (Crockford said it is a design error) that inside a function, arguments is like an array, except it is not a real array, so array methods cannot be invoked on it:

function foo() { console.log(arguments.slice(1)) }   // won't work
foo(1,2,3);

我刚刚在最新的Firefox和Chrome上试过它,但它不适用于两者。
所以我们可能必须使用

And I just tried it on the latest Firefox and Chrome, and it won't work on both. So we may have to use

function foo() { console.log(Array.prototype.slice.call(arguments, 1)) }
foo(1,2,3);

但为什么不让参数一个真正的数组在现代JavaScript?可能不应该有任何程序依赖于参数不是真正的数组?现在可能有什么理由不让它成为一个真正的数组?

But why not make arguments a real array in the modern JavaScript? There probably shouldn't be any program that depends on arguments not being a real array? What might be a reason not to make it a real array now?

我能想到的一个原因是,如果程序员开始把它当成一个数组,那么代码就赢了在旧版浏览器中工作,但ECMA-5中的其他东西也不适用于旧浏览器。

One reason I can think of is, if programmers start treating it as an array, then the code won't work in older browsers, but there are other things in ECMA-5 that won't work in older browsers too.

推荐答案

直到ECMAScript 5开发的最后阶段,参数对象才会继承所有的Array.prototype方法。但TC39于2009年9月批准的ES5最终草案没有此功能。

Until very late in the development of ECMAScript 5, argument object were going to inherit all of the Array.prototype methods. But the "final draft" of ES5 approved by TC39 in Sept. 2009 did not have this feature.

2009年8月,Apple的Oliver Hunt将其发布到了es5 -discuss邮件列表

In August 2009, Oliver Hunt of Apple posted this to the the es5-discuss mailing list

https://mail.mozilla.org/pipermail/es5-discuss/2009-August/003112.html


我实现了逻辑,使Arguments对象在上周五的WebKit中从Array
继承,很快就发现了与Prototype.js
... $ $
的不兼容性b $ b这打破了至少一些Apple网站和Nasa.gov -
...
由于这些网站破坏,主要兼容性问题引起的问题

在一个相当主要的库中,似乎不可能尝试将
数组的行为改为参数。

I implement the logic to make the Arguments object inherit from Array in WebKit last friday and it's quickly turned up a severe
incompatibility with Prototype.js ... This breaks at the very least a number of Apple sites and Nasa.gov -- ... Due to these site breakages, caused by a major compatibility problem
in a fairly major library it seems infeasible to attempt to retrofit
array like behaviour onto arguments.

其余的TC39同意Oliver的评估和fe ature已从最终草案中删除。

The rest of TC39 agreed with Oliver's assessment and the feature was removed from the final draft.

或许,这样的代码已经从网络中消失得足以使提议的ES5解决方案今天起作用。但是,这并不重要,因为ES6中的其他参数是解决问题的更好解决方案,也是一种全新的语法功能,不会对现有代码产生任何向后兼容性问题。

Perhaps, such code has disappeared sufficiently from the web that the proposed ES5 solution would work today. However, it doesn't matter because rest parameters in ES6 is a better solution to the problem and a completely new syntactic feature that can't have any backwards compatibility issues with existing code.

这篇关于对于Javascript,什么阻止现代实现不将参数视为真正的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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