什么是 arguments 变量的实例?
MDN 说它是类似对象的数组" 但没有说明它是什么实例.
MDN says it is "an Array like object" but does not say what it is an instance of.
它不是 HTMLCollection
或 NodeList
.
如果我调用 Object.prototype.toString.call(arguments)
它返回 "[object Arguments]"
但 arguments instanceof Arguments
是一个错误.
If I call Object.prototype.toString.call(arguments)
it returns "[object Arguments]"
but arguments instanceof Arguments
is an error.
那么 arguments
的实例是什么?
So what is arguments
an instance of?
推荐答案
那么参数的实例是什么?
So what is arguments an instance of?
它是 Object
的一个实例.似乎没有任何公共 Arguments
构造函数可以与 instanceof
一起使用来以这种方式识别它.
It's an instance of Object
. There does not appear to be any public Arguments
constructor that you can use with instanceof
to identify it that way.
如果你想唯一标识它,那么:
If you want to uniquely identify it, then:
Object.prototype.toString.call(arguments) === "[object Arguments]"
是一种安全的识别方式.
is a safe way to identify it.
根据 EcmaScript 6 规范中的 第 9.4.4 节,arguments
对象要么是普通对象,要么是奇异对象.规范是这样说的:
Per section 9.4.4 in the EcmaScript 6 specification, the arguments
object is either an ordinary object or an exotic object. Here's what the spec says:
大多数 ECMAScript 函数都使参数对象可用于它们的代码.根据函数定义的特点,它的参数对象要么是一个普通对象,要么是一个参数异国情调的物体.
Most ECMAScript functions make an arguments objects available to their code. Depending upon the characteristics of the function definition, its argument object is either an ordinary object or an arguments exotic object.
异国情调的论据object 是一个奇异的对象,其数组索引属性映射到调用其关联的形式参数绑定ECMAScript 函数.
An arguments exotic object is an exotic object whose array index properties map to the formal parameters bindings of an invocation of its associated ECMAScript function.
参数外来对象具有相同的内部插槽作为普通对象.它们还有一个 [[ParameterMap]] 内部投币口.普通参数对象也有一个 [[ParameterMap]] 内部其值始终未定义的插槽.对于普通参数对象[[ParameterMap]] 内部插槽仅由Object.prototype.toString (19.1.3.6) 来识别它们.
Arguments exotic objects have the same internal slots as ordinary objects. They also have a [[ParameterMap]] internal slot. Ordinary arguments objects also have a [[ParameterMap]] internal slot whose value is always undefined. For ordinary argument objects the [[ParameterMap]] internal slot is only used by Object.prototype.toString (19.1.3.6) to identify them as such.
由于它是一个异国情调"的对象,这本质上意味着它不遵循所有正常和预期的约定.例如,没有可以用来创建自己的对象的构造函数.而且,由于没有公共构造函数,这可能也解释了为什么没有您可以对其进行测试以唯一标识它的 instanceof
.
Since it is an "exotic" object, that essentially means it doesn't follow all the normal and expected conventions. For example, there is no constructor function from which you can create your own object. And, because there is no public constructor function, that probably also explains why there's no instanceof
that you can test on it to uniquely identify it.
相关文章