DOM4がNodeListの反復可能になり:for(Symbol.iterator)でHTMLCollectionを反復できますか?
interface NodeList {
getter Node ? item (unsigned long index);
readonly attribute unsigned long length ;
iterable< Node >;
};
WebIDLによれば、これは、値の配列を取得するために反復処理される反復可能 支持体であることが宣言されているインターフェースを実装
オブジェクトを意味します。
注のECMAScript言語の結合、反復可能なそのinterface prototype objectの「エントリ」、「forEachの」、「キー」、「値」と @@iterator特性を有する あるインターフェース。
したがって、次が可能です:
for (var el of document.querySelectorAll(selector)) ...
私は同じことが、両方のFirefoxとChromeで、HTMLCollectionsのために働くようだ気づい:実際には
for (var el of document.getElementsByTagName(tag)) ...
、私は
を取得HTMLCollection.prototype[Symbol.iterator] === [][Symbol.iterator]; // true
ただし、HTMLCollectionはありません反復可能として定義:
interface HTMLCollection {
readonly attribute unsigned long length ;
getter Element ? item (unsigned long index);
getter Element ? namedItem (DOMString name);
};
は、私はまた、WHATWG DOM specをチェックし、それが反復可能でもないではありません。
次に、この動作基準はありますか? @@iterator
がプロトタイプにあるとしますか?HTMLCollection
はありますか? for..of
と...spread
それは@@iterator
を保存プロパティに対して[[Get]]
操作のためのイテレータを返す場合何が反復可能である:事実上iterable
の構造を持っているすべてのもののようないくつかの反復エンジン操作を経ることができ、JavaScriptで
http://stackoverflow.com/a/31574921/2813224 – zer00ne