简体   繁体   English

ES6 / Babel传播错误与Set

[英]ES6 / Babel spread error with Set

I have the following code, simplified from a real-world example. 我有以下代码,从现实世界的例子中简化。

let arr = [0,1,2],
    s = new Set(arr);

let arr2 = [...s];
alert('3 == ' + arr2.length);

The problem is that this fails, and produces an empty arr2 on Google Chrome which has a native Set implementation, but a polyfilled Array.from. 问题是这会失败,并在Google Chrome上生成一个空的arr2,它具有一个本机Set实现,但是一个polyfilled Array.from。 Amusingly, it works properly on IE11, which has a polyfilled Array.from AND Set. 有趣的是,它在IE11上运行正常,它有一个polyfilled Array.from AND Set。

Babel converts the spread Set to this Babel将展开集转换为此

var arr2 = [].concat(_toConsumableArray(s));

and _toConsumableArray returns Array.from . _toConsumableArray返回Array.from I've set a breakpoint inside of _toConsumableArray and I can see it producing an empty Array through the call to Array.from . 我在_toConsumableArray设置了一个断点,我可以通过调用Array.from看到它产生一个空数组。

My question is, is this a bug in the Array.from polyfill, in that it doesn't properly process a native (not polyfilled) Set, or is the problem with the Babel code, in that Array.from(x) is not a perfect equivalent for ...x (when x is not an array). 我的问题是,这是否是Array.from polyfill中的一个错误,因为它没有正确处理本机(不是polyfilled)Set,或者是Babel代码的问题,因为Array.from(x)不是完美等效于...x (当x不是数组时)。

I can see it producing an empty Array through the call to Array.from . 我可以看到它通过调用Array.from生成一个空数组。 Is this a bug in MDN's Array.from polyfill? 这是MDN的Array.from的错误吗?

Not really a bug, but actually properly documented : 不是真正的错误,但实际上是正确记录的

In addition, since true iterables can not be polyfilled, this implementation does not support generic iterables as defined in the 6th edition of ECMA-262. 此外,由于真正的迭代不能被填充,因此该实现不支持ECMA-262第6版中定义的通用迭代。

I assume that refers to the lack of Symbol.iterator in ES5. 我认为这是指ES5中缺少Symbol.iterator

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM