Javascript迭代的技术定义是什么?您如何测试? [英] What is the technical definition of a Javascript iterable and how do you test for it?
问题描述
我一直在实现一个有用的子类ES6 Set
对象。对于我的许多新方法,我想接受一个可以是另一个Set或者Array的参数,或者我可以重复的任何东西。我已经在我的界面中调用了一个可迭代的,只需使用 .forEach()
(对于一个集合或一个数组,它可以正常工作)示例代码: / p>
//删除此集中位于otherIterable中的项目
//返回删除的项目数量
remove(otherIterable){
let cnt = 0;
otherIterable.forEach(item => {
if(this.delete(item)){
++ cnt;
}
});
return cnt;
}
或
//将所有项目从其他可迭代添加到此集合
pre>
addTo(iterable) {
iterable.forEach(item => {
this.add(item);
});
}
但是,我怀疑我可能不会以ES6定义的方式真正支持任何迭代,所以我对一个Javascript迭代的真正定义感兴趣使用该术语作为ES6规范?
何你可以在ES6 Javascript中测试吗?
如何迭代一个通用的迭代?
我在ES6规范中发现了这样的短语:
如果参数iterable存在,那么它应该是一个对象
,它实现一个@@ iterator方法,返回一个迭代器对象
生成一个两元素的数组类对象,其第一个元素是一个
值,它将被用作WeakMap键,第二个元素是
与该键相关联的值。
但是,它指的是一个
@@ iterator方法
似乎可以通过该属性名访问。解决方案
什么是真正的定义一个使用该术语作为ES6规范的Javascript迭代?
§25.1.1。 1 定义了 Iterable 接口。
它们是具有
Symbol.iterator
-keyed方法返回一个有效的迭代器(这反过来是一个预期的行为,因为它应该根据§25.1.1.2)。
如何在ES6 Javascript中测试?
我们无法测试
@@ iterator
方法返回而不调用它,我们无法测试结果是否符合Iterator
接口,而不尝试运行它。最好的选择是做function looksIntable(o){
return typeof o [Symbol.iterator] = =功能;
}
然而,我通常不会测试这个,但只是让它失败,
如何迭代一个通用的迭代?
不要使用
forEach
。(实际上,永远不要使用forEach
ES6中的任何地方)
正确的迭代方式是(... of ...)的
循环。它执行所有的迭代检查(使用摘要的
GetIterator
操作,并运行(甚至关闭)迭代器,并在非迭代值使用时抛出适当的TypeError
。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; }
Or
// add all items from some other iterable to this set addTo(iterable) { iterable.forEach(item => { this.add(item); }); }
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?
How do you test for it in ES6 Javascript?
How should you iterate a generic iterable?
I've found phrases like this in the ES6 specification:
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.
But, that refers to an
@@iterator method
which I don't seem to be able to access via that property name.解决方案What is the real definition of a Javascript iterable using the term as the ES6 specification does?
§25.1.1.1 defines "The Iterable Interface".
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).How do you test for it in ES6 Javascript?
We cannot test what the
@@iterator
method returns without calling it, and we cannot test whether the result conforms to theIterator
interface without trying to run it. The best bet would be to dofunction 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?
Don't use
forEach
. (In fact, never useforEach
anywhere in ES6).The proper way to iterate is a
for (… of …)
loop. It does all the checking for iterability (using the abstractGetIterator
operation and running (and even closing) the iterator, and throws appropriateTypeError
s when used on non-iterable values.这篇关于Javascript迭代的技术定义是什么?您如何测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!