forEach method of Node.childNodes?

最后都变了- 提交于 2019-11-26 22:52:01

DOM4 now defines NodeList as an iterable:

iterable<Node>;

According to the IDL draft, that means

An interface can be declared to be iterable by using an iterable declaration (matching Iterable) in the body of the interface.

iterable<value-type>;
iterable<key-type, value-type>;

Objects implementing an interface that is declared to be iterable support being iterated over to obtain a sequence of values.

Note: In the ECMAScript language binding, an interface that is iterable will have “entries”, “forEach”, “keys”, “values” and @@iterator properties on its interface prototype object.

If a single type parameter is given, then the interface has a value iterator and provides values of the specified type.

Is the method available only at relatively recent versions of Chrome / Chromium? If yes, is this documented?

Yes, this is new in DOM4, so not widely available.

Is there any documentation concerning the forEach() method of Node.childNodes?

See Add support for [ArrayClass] and use that on NodeList on the Chromium bug tracker:

From https://bugs.webkit.org/show_bug.cgi?id=81573

http://dom.spec.whatwg.org/#interface-nodelist

DOM4 specs NodeList as having Array.prototype in its prototype chain.

Some more background for this one. [ArrayClass] allows us to do things like document.querySelectorAll('.foo').forEach etc. The patch at bugs.webkit.org has a runtime flag because it is unclear if this will still be possible to achieve.

Historically these array-like objects did not include these methods from the array prototype, leading to code like Array.prototype.forEach.call(nodeList, function() { ... }). This is now meant to change in DOM4.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!