問(wèn)題描述
我一直在實(shí)現(xiàn) ES6 Set
對(duì)象的一個(gè)??有用的子類(lèi).對(duì)于我的許多新方法,我希望接受一個(gè)參數(shù),該參數(shù)可以是另一個(gè) Set 或 Array,或者實(shí)際上是我可以迭代的任何東西.我一直在我的界面中將其稱(chēng)為可迭代",并在其上使用 .forEach()
(這對(duì)于 Set 或 Array 來(lái)說(shuō)很好.示例代碼:
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 定義的任何可迭代對(duì)象,所以我對(duì) Javascript 可迭代對(duì)象的真正定義使用 ES6 規(guī)范中的術(shù)語(yǔ)感興趣?
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 中對(duì)其進(jìn)行測(cè)試?
How do you test for it in ES6 Javascript?
你應(yīng)該如何迭代一個(gè)通用的可迭代對(duì)象?
How should you iterate a generic iterable?
我在 ES6 規(guī)范中發(fā)現(xiàn)了這樣的短語(yǔ):
I've found phrases like this in the ES6 specification:
如果參數(shù)iterable存在,它應(yīng)該是一個(gè)對(duì)象實(shí)現(xiàn)了一個(gè)返回迭代器對(duì)象的@@iterator 方法生成一個(gè)二元素類(lèi)數(shù)組對(duì)象,其第一個(gè)元素是將用作 WeakMap 鍵的值,其第二個(gè)元素是與該鍵關(guān)聯(lián)的值.
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.
但是,這指的是一個(gè) @@iterator 方法
,我似乎無(wú)法通過(guò)該屬性名稱(chēng)訪問(wèn)它.
But, that refers to an @@iterator method
which I don't seem to be able to access via that property name.
推薦答案
像 ES6 規(guī)范那樣使用術(shù)語(yǔ)的 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 方法的對(duì)象,該方法返回一個(gè)有效的 Iterator (這反過(guò)來(lái)又是一個(gè)預(yù)期按照 §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 中對(duì)其進(jìn)行測(cè)試?
How do you test for it in ES6 Javascript?
不調(diào)用@@iterator
方法就無(wú)法測(cè)試返回的內(nèi)容,不嘗試運(yùn)行就無(wú)法測(cè)試結(jié)果是否符合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";
}
但是我通常不會(huì)對(duì)此進(jìn)行測(cè)試,而只是在它不可迭代時(shí)讓它失敗并拋出異常.
however I wouldn't usually test for this but simply let it fail with an exception when it's not iterable.
你應(yīng)該如何迭代一個(gè)通用的可迭代對(duì)象?
How should you iterate a generic iterable?
不要使用 forEach
.(事實(shí)上,不要在 ES6 的任何地方使用 forEach
).
正確的迭代方式是 for (... of ...)
循環(huán).它會(huì)檢查所有的可迭代性(使用抽象的 GetIterator
操作 并運(yùn)行(甚至關(guān)閉)迭代器,并在用于不可迭代的值時(shí)拋出適當(dāng)?shù)?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 TypeError
s when used on non-iterable values.
這篇關(guān)于Javascript 可迭代的技術(shù)定義是什么,您如何對(duì)其進(jìn)行測(cè)試?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!