Posted By

Sephr on 02/23/09


Tagged

object prototype foreach js18


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

enajenkins
kesla


object.forEach [JS 1.8]


 / Published in: JavaScript
 

object.forEach

Requires JavaScript 1.8

void object.forEach(function callback(item, value)[, thisObject])

  1. Object.prototype.forEach = function(fun /*, thisp*/) {
  2. // if this gets called on an array, use appropriate forEach
  3. if ( Object.prototype.toString.call(this) == "[object Array]" ) return Array.prototype.forEach.apply(this, Array.prototype.slice.call(arguments));
  4. if ( typeof fun != "function" ) throw new TypeError();
  5. let it = Iterator(this), thisp = arguments[1];
  6. try {
  7. while (true) {
  8. fun.apply(thisp, it.next());
  9. }
  10. } catch (err if err instanceof StopIteration) {}
  11. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Piotrek290 on August 29, 2009

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

You need to login to post a comment.