본래의 방법은이 작업을 수행하는 것입니다. 웹킷에서. Firefox에서는 그렇지 않습니다. FF는 HTMLCollection ...
내가 찾은 가장 크로스 브라우저 방식을 반환하므로 :
NodeList.prototype.forEach = HTMLCollection.prototype.forEach = Array.prototype.forEach;
그것은 IE8에서 작동하고 호스트 오브젝트에 속성을 추가 할 때 그들은 숨이 있기 때문에, 비록 낮은되지 않습니다 프로토 타입.
전체 목록 :
NodeList.prototype.forEach = HTMLCollection.prototype.forEach = Array.prototype.forEach;
NodeList.prototype.map = HTMLCollection.prototype.map = Array.prototype.map;
NodeList.prototype.filter = HTMLCollection.prototype.filter = Array.prototype.filter;
NodeList.prototype.reduce = HTMLCollection.prototype.reduce = Array.prototype.reduce;
NodeList.prototype.reduceRight = HTMLCollection.prototype.reduceRight = Array.prototype.reduceRight;
NodeList.prototype.every = HTMLCollection.prototype.every = Array.prototype.every;
NodeList.prototype.some = HTMLCollection.prototype.some = Array.prototype.some;
또는은, 우리의 친애하는 BERGI을 기쁘게하기 위해 (그리고 또한 청소기이기 때문에) :
['forEach', 'map', 'filter', 'reduce', 'reduceRight', 'every', 'some'].forEach(
function(p) {
NodeList.prototype[p] = HTMLCollection.prototype[p] = Array.prototype[p];
});
perfectionkills에 대한 링크를 고려하면, 그것은이 대부분 무관합니다. 문제는 확장 될 때 DOM이 브라우저에서 거의 동일하게 작동하지 않는다는 것입니다. 이 수정은 IE < = 8을 제외한 모든 브라우저에서 정상입니다.
왜 모든 사용자가'Array.prototype.forEach'를 사용하는 대신 배열 ('[]')을 할당합니까? –