Skip to content

Instantly share code, notes, and snippets.

@triacontane
Last active January 18, 2016 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save triacontane/d4dc646616628ad5731b to your computer and use it in GitHub Desktop.
Save triacontane/d4dc646616628ad5731b to your computer and use it in GitHub Desktop.
オブジェクトのプロパティに対して繰り返し処理を行う
if (!Object.prototype.hasOwnProperty('iterate')) {
Object.defineProperty(Object.prototype, 'iterate', {
value : function (handler) {
Object.keys(this).forEach(function (key, index) {
handler.call(this, key, this[key], index);
}, this);
}
});
}
var a = {};
a.aaa = 100;
a.bbb = 200;
a.iterate(function(key, item, index) {
console.log(key + ':' + item + ':' index);
}.bind(this));
// aaa:100:0
// bbb:200:1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment