问题描述
我一直在实现 es6 set 对象的一个??有用的子类.对于我的许多新方法,我希望接受一个参数,该参数可以是另一个 set 或 array,或者实际上是我可以迭代的任何东西.我一直在我的界面中将其称为可迭代",并在其上使用 .foreach() (这对于 set 或 array 来说很好.示例代码:
i've been implementing a useful subclass of the es6 set object. for many of my new methods, i want to accept an argument that can be either another set or an array, or really anything that i can iterate. i've been calling that an "iterable" in my interface and just use .foreach() on it (which works fine for a set or an array. example code:
// remove items in this set that are in the otheriterable // returns a count of number of items removed remove(otheriterable) { let cnt = 0; otheriterable.foreach(item => { if (this.delete(item)) { cnt; } }); return cnt; }
或者
// add all items from some other iterable to this set addto(iterable) { iterable.foreach(item => { this.add(item); }); }
但是,我怀疑我可能并不真正支持 es6 定义的任何可迭代对象,所以我对 javascript 可迭代对象的真正定义使用 es6 规范中的术语感兴趣?
but, i suspect i may be not really supporting any iterable in the way that es6 defines it so i'm interested in what the real definition of a javascript iterable is using the term as the es6 specification does?
如何在 es6 javascript 中对其进行测试?
how do you test for it in es6 javascript?
你应该如何迭代一个通用的可迭代对象?
how should you iterate a generic iterable?
我在 es6 规范中发现了这样的短语:
i've found phrases like this in the es6 specification:
如果参数iterable存在,它应该是一个对象实现了一个返回迭代器对象的@@iterator 方法生成一个二元素类数组对象,其第一个元素是将用作 weakmap 键的值,其第二个元素是与该键关联的值.
if the parameter iterable is present, it is expected to be an object that implements an @@iterator method that returns an iterator object that produces a two element array-like object whose first element is a value that will be used as a weakmap key and whose second element is the value to associate with that key.
但是,这指的是一个 @@iterator 方法,我似乎无法通过该属性名称访问它.
but, that refers to an @@iterator method which i don't seem to be able to access via that property name.
推荐答案
像 es6 规范那样使用术语的 javascript 可迭代的真正定义是什么?
what is the real definition of a javascript iterable using the term as the es6 specification does?
§25.1.1.1 定义"iterable 接口".
它们是带有 symbol.iterator-keyed 方法的对象,该方法返回一个有效的 iterator (这反过来又是一个预期按照 §25.1.1.2).
they're objects with a symbol.iterator-keyed method that returns a valid iterator (which in turn is an object expected to behave as it should according to §25.1.1.2).
如何在 es6 javascript 中对其进行测试?
how do you test for it in es6 javascript?
不调用@@iterator方法就无法测试返回的内容,不尝试运行就无法测试结果是否符合iterator接口.最好的办法是这样做
we cannot test what the @@iterator method returns without calling it, and we cannot test whether the result conforms to the iterator interface without trying to run it. the best bet would be to do
function looksiterable(o) { return typeof o[symbol.iterator] == "function"; }
但是我通常不会对此进行测试,而只是在它不可迭代时让它失败并抛出异常.
however i wouldn't usually test for this but simply let it fail with an exception when it's not iterable.
你应该如何迭代一个通用的可迭代对象?
how should you iterate a generic iterable?
不要使用 foreach.(事实上,不要在 es6 的任何地方使用 foreach).
正确的迭代方式是 for (... of ...) 循环.它会检查所有的可迭代性(使用抽象的 getiterator 操作 并运行(甚至关闭)迭代器,并在用于不可迭代的值时抛出适当的 typeerror.
the proper way to iterate is a for (… of …) loop. it does all the checking for iterability (using the abstract getiterator operation and running (and even closing) the iterator, and throws appropriate typeerrors when used on non-iterable values.