Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Forked from cowboy/object-forin-forown.js
Created June 5, 2012 17:50
Show Gist options
  • Save rwaldron/2876537 to your computer and use it in GitHub Desktop.
Save rwaldron/2876537 to your computer and use it in GitHub Desktop.
JavaScript: Object#forIn ?
Object.defineProperty(Object.prototype, 'forIn', {
value: function(iterator, thisValue) {
Object.keys(this).forEach(function(key) {
iterator.call(thisValue, this[key], key, this);
}.bind(this));
}
});
var obj = {a: 1, b: 2};
obj.forIn(console.log, console);
// 1 'a' { a: 1, b: 2 }
// 2 'b' { a: 1, b: 2 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment