Last active
September 27, 2015 20:42
-
-
Save qetr1ck-op/188c0afa47da9b036b73 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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