Last active
June 29, 2016 00:05
-
-
Save fijiwebdesign/dab41ba5878b4cdfed4c687606a96085 to your computer and use it in GitHub Desktop.
Object keys iteration with forEach
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
// iteration using for..in loop and hasOwnProperty | |
var Person = { | |
name: 'mr smith', | |
profession: 'dandy fella', | |
age: 10000, | |
reset: function() { | |
for (var prop in this) { | |
if (prop == 'reset' || !this.hasOwnProperty(prop)) { | |
console.log("leave it alone", prop) | |
} else { | |
this[prop] = null | |
} | |
} | |
} | |
} |
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
// Iteration using forEach | |
var Person = { | |
name: 'mr smith', | |
profession: 'dandy fella', | |
age: 10000, | |
reset: function() { | |
Object.keys(this).forEach(function(prop) { | |
if (prop != 'reset') { | |
this[prop] = null; // this now references Person | |
} | |
}, this) // this references Person | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment