arguments对象在ES6中应该是可迭代的

Is the arguments object supposed to be an iterable in ES6?

本文关键字:迭代 对象 ES6 arguments      更新时间:2023-09-26

在ES6中,当传递给Set构造函数时,我试图将arguments对象用作可迭代对象。它在IE11和Chrome 47中运行良好。它在Firefox 43中不起作用(抛出一个TypeError: arguments is not iterable)。我已经查看了ES6规范,但并没有真正找到arguments对象是否应该是可迭代对象的定义。

下面是我尝试做的一个例子:

function destroyer(arr) {
  var removes = new Set(arguments);
  return arr.filter(function(item) {
    return !removes.has(item);
  });
}
// remove items 2, 3, 5 from the passed in array
var result = destroyer([3, 5, 1, 2, 2], 2, 3, 5);
log(result);

仅供参考,我知道这段代码有各种解决方法,例如将arguments对象复制到实际数组中或使用rest参数。这个问题是关于arguments对象是否应该是ES6中的iterable,它可以在任何期望迭代的地方使用。

我想这是FF实现中的一个错误。

根据9.2.12函数声明实例化(函数,参数列表)部分,

如果argumentsObjectNeeded为true,则

a) 如果strict为true或simpleParameterList为false,则

===>i) 设ao为CreateUnmappedArgumentsObject(argumentsList)。

b) 否则,

===>i) 注:映射的参数对象只提供给没有rest参数、任何参数默认值初始值设定项或任何析构函数参数的非严格函数。

===>ii)设ao为CreateMappedArgumentsObject(函数、形式、参数列表、env)。

CreateMappedArgumentsObjectCreateUnmappedArgumentsObject都具有

执行DefinePropertyOrThrow(obj,@@iterator,PropertyDescriptor{[[Value]]:%ArrayProto_values%,[[Writable]]:true,[[Enumerable]]:false,[[Configuration]]:true})。

这意味着@@iterator属性实际上应该已经在arguments对象上定义。