微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 参数对象是否应该在ES6中是可迭代的?

在ES6中,当我传递给Set构造函数时,我试图使用arguments对象作为一个迭代.它可以在IE11和Chrome 47中正常工作.它在Firefox 43中不起作用(抛出一个TypeError:arguments不是可迭代的).我查看了ES6规范,并不能真正找到一个定义,参数对象是否应该是可迭代的.

这是我正试图做的一个例子:

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],5);
log(result);

FYI,我知道这个代码有各种各样的解决方案,比如将参数对象复制到一个真实的数组或者使用rest参数.这个问题是关于参数对象在ES6中是否应该是可迭代的,可以在任何地方使用迭代.

解决方法

我想这是FF的一个bug.

根据9.2.12 FunctionDeclarationInstantiation(func,argumentsList)部分,

If argumentsObjectNeeded is true,then

a) If strict is true or if simpleParameterList is false,then

==> i) Let ao be CreateUnmappedArgumentsObject(argumentsList).

b) Else,

==> i) NOTE mapped argument object is only provided for non-strict functions that don’t have a rest parameter,any parameter default value initializers,or any destructured parameters .

==> ii) Let ao be CreateMappedArgumentsObject(func,formals,argumentsList,env).

CreateMappedArgumentsObjectCreateUnmappedArgumentsObject都有

Perform DefinePropertyOrThrow(obj,@@iterator,PropertyDescriptor {[[Value]]:%ArrayProto_values%,[[Writable]]: true,[[Enumerable]]: false,[[Configurable]]: true}).

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

原文地址:https://www.jb51.cc/js/153542.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐