为什么函数的参数对象不是 Javascript 中的数组? [英] Why isn't a function's arguments object an array in Javascript?

查看:29
本文介绍了为什么函数的参数对象不是 Javascript 中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为看起来人们做的第一件事似乎是将 arguments 转换成一个真正的数组,所以我对 Javascript 语言作者和实现者为什么决定并继续思考 感兴趣参数 应该是一个真正的Array.我的意思不是说这是一个火焰诱饵,我对它背后的想法很感兴趣.由于该函数在您处于其主体中时自然会被调用,因此我认为这不是因为 arguments 所引用的对象可以更改,例如某些 DOM 结果...

Since it seems like the first thing people do is convert arguments into a real array, I'm interested in why the Javascript language authors and implementers decided, and continue to think, that arguments should not be a real Array. I don't mean this as flamebait, I'm sincerely interested in the thinking behind it. Since the function is naturally being called when you're in its body, I don't think it's because the objects arguments are referencing can change, like with some of the DOM results...

推荐答案

我的猜想:

arguments 对象的概念从一开始就存在于语言中,它甚至在 ECMAScript 第一版标准(PDF).

The concept of the arguments object has been on the language since the very beginning, it's even described in the ECMAScript First Edition Standard(PDF).

在那个版本的 ECMAScript 中,Array.prototype 非常基础,数组对象只包含 4 个方法!:toStringjoinreversesort.

In that version of ECMAScript, the Array.prototype was really basic, array objects contained only 4 methods!: toString, join, reverse and sort.

我认为这是他们让arguments继承自Object.prototype的主要原因之一,当时那些Array方法看起来不太有用.

I think that's one of the major reasons about they make arguments to inherit from Object.prototype, at that time those Array methods didn't look too useful.

但是Array.prototype对象在标准的下一个版本中被扩展了,现在在ES5上,Array对象有mapreduce<等方法/code>、everysome 等,非常强大.

But the Array.prototype object was extended in the next versions of the standard, now on ES5, Array objects have methods such as map, reduce, every, some, etc, that are really powerful.

去年,在标准的草案阶段,在 ES5 中提出了让 arguments 继承自 Array.prototype 的提议,但后来被放弃了.

The last year, there was a proposal in ES5 to make arguments inherit from Array.prototype, in the draft stages of the standard, but was dropped off time later.

在那些草稿中,arguments 继承自 Array.prototype,但为了向后兼容 ES3,arguments 对象定义了两个自己的属性、toStringtoLocaleString,都指向Object.prototype 上的相同方法,但最终委员会决定继续继承Object.prototype.

In those drafts, arguments inherited from Array.prototype, but for backwards compatibility with ES3, the arguments object had defined two own properties, toString and toLocaleString, both pointing to the same methods on Object.prototype, but finally, the committee decided to keep inheriting from Object.prototype.

这篇关于为什么函数的参数对象不是 Javascript 中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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