/ Published in: JavaScript
object.forEach
Requires JavaScript 1.8
void object.forEach(function callback(item, value)[, thisObject])
Expand |
Embed | Plain Text
Object.prototype.forEach = function(fun /*, thisp*/) { // if this gets called on an array, use appropriate forEach if ( Object.prototype.toString.call(this) == "[object Array]" ) return Array.prototype.forEach.apply(this, Array.prototype.slice.call(arguments)); if ( typeof fun != "function" ) throw new TypeError(); let it = Iterator(this), thisp = arguments[1]; try { while (true) { fun.apply(thisp, it.next()); } } catch (err if err instanceof StopIteration) {} }
Comments
Subscribe to comments
You need to login to post a comment.

Hi, I prefer this code: Array.prototype.foreach = function(callback){ var that = this; for(var i=0; i