Skip to content

Instantly share code, notes, and snippets.

@qetr1ck-op
Last active September 27, 2015 20:42
Show Gist options
  • Save qetr1ck-op/188c0afa47da9b036b73 to your computer and use it in GitHub Desktop.
Save qetr1ck-op/188c0afa47da9b036b73 to your computer and use it in GitHub Desktop.
/*
1. create prototype object
2. should itterate through object, aka forEach only for objects
obj.each(function(prop, val) {
console.log( prop ); // name -> age
});
*/
Object.prototype.each = function(f) {
for (var prop in this) {
// iterate only through own property
if (!this.hasOwnProperty(prop)) continue;
var value = this[prop];
f.call(this, prop, value);
}
};
Object.defineProperty(Object.prototype, 'each', {
enumerable: false
});
var obj = {
name: 'Bobby',
age: 25
};
obj.each(function(prop, val) {
console.log( prop ); // name -> age
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment