2 ms·
Underscore : _.each(array, iteratee) ES5.1 : array.forEach(iteratee) And all the other elements... Yes, you are right. But... The "array" is not always an Arr
by fabien_ 11y ago
Underscore : _.each(array, iteratee)
ES5.1 : array.forEach(iteratee)
And all the other elements...
Yes, you are right. But... The "array" is not always an Array. document.getElementsByClassName( 'myClass' ) is not an Array but a DOMNodeList. So document.getElementsByClassName( 'myClass' ).forEach(iteratee) will not work. Same for map, reduce...
Underscore (or lodash) makes more that just polyfilling es5 methods.
- julienng 11y agoDo something like: NodeList.prototype.__proto__ = Array.prototype;
- paulcpederson 11y agovar myClass = document.getElementsByClassName('myClass') Array.prototype.slice.call(myClass).forEach(iteratee)