Permalink
Browse files

Implemented an object.forEach. I benchmarked this and it's actually f…

…aster than a plain for..in loop over an object.
  • Loading branch information...
1 parent 8cc2ab4 commit 47a3aeb915817c2407444623cdc6dddc2aeae9e1 @creationix committed Mar 18, 2010
Showing with 17 additions and 0 deletions.
  1. +17 −0 lib/proto.js
View
@@ -0,0 +1,17 @@
+var proto = Object.prototype;
+
+// Implements a forEach much like the one for Array.prototype.forEach, but for
+// and object.
+if (typeof proto.forEach !== 'function') {
+ Object.defineProperty(proto, "forEach", {enumarable: false, value: function (callback, thisObject) {
+ var keys = Object.keys(this);
+ var length = keys.length;
+ sys.p(keys);
+ for (var i = 0; i < length; i++){
+ key = keys[i];
+ callback.call(thisObject, this[key], key, this);
+ }
+ }});
+}
+
+

0 comments on commit 47a3aeb

Please sign in to comment.