--- title: NodeList.prototype.forEach() slug: Web/API/NodeList/forEach translation_of: Web/API/NodeList/forEach ---
{{APIRef("DOM")}}
{{domxref("NodeList")}} 인터페이스의 forEach() 메서드는 리스트 내의 각각의 값 쌍에 대해 매개 변수에 지정된 콜백을 삽입 순서로 호출합니다.
NodeList.forEach(callback[, thisArg]);
callbackcurrentValuecurrentIndexlistObjforEach() 가 적용되고 있는 NodeList 객체입니다. thisArg {{Optional_inline}}callback 을 실행할 때 {{jsxref("this")}} 에 대입할 값입니다.{{jsxref('undefined')}}.
None.
var node = document.createElement("div");
var kid1 = document.createElement("p");
var kid2 = document.createTextNode("hey");
var kid3 = document.createElement("span");
node.appendChild(kid1);
node.appendChild(kid2);
node.appendChild(kid3);
var list = node.childNodes;
list.forEach(
function(currentValue, currentIndex, listObj) {
console.log(currentValue + ', ' + currentIndex + ', ' + this);
},
'myThisArg'
);
결과는 다음과 같습니다.
[object HTMLParagraphElement], 0, myThisArg [object Text], 1, myThisArg [object HTMLSpanElement], 2, myThisArg
이 {{Glossary("Polyfill","polyfill")}} 은 ES5 를 지원하는 모든 브라우저에서 동작합니다:
if (window.NodeList && !NodeList.prototype.forEach) {
NodeList.prototype.forEach = function (callback, thisArg) {
thisArg = thisArg || window;
for (var i = 0; i < this.length; i++) {
callback.call(thisArg, this[i], i, this);
}
};
}
또는
if (window.NodeList && !NodeList.prototype.forEach) {
NodeList.prototype.forEach = Array.prototype.forEach;
}
The above behavior is how many browsers actually implement NodeList.prototype.forEach (Chrome, for example).
| Specification | Status | Comment |
|---|---|---|
| {{SpecName("WebIDL", "#es-forEach", "forEach")}} | {{Spec2("WebIDL")}} | Defines forEach on iterable declarations |
{{Compat("api.NodeList.forEach")}}