Last active
January 18, 2016 17:11
-
-
Save triacontane/d4dc646616628ad5731b to your computer and use it in GitHub Desktop.
オブジェクトのプロパティに対して繰り返し処理を行う
This file contains 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
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