Created
April 12, 2011 14:08
-
-
Save bodbdigr/915548 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
//extending the object prototype | |
Object.prototype.newMethod = function() { | |
return this; | |
}; | |
//create new Object | |
var names = {'Jane': 'Joe', 'John': 'Doe'}; | |
//iterating the Object would give: Jane => Doe, John => Doe, newMethod => function(); | |
for(var key in names) { | |
console.log(key, '=>', names[key], ','); | |
} | |
//thats why object.hasOwnProperty should be called on each iteration. | |
//It checks whatever key exists on object itself not it prototypes chain. | |
//iterating the Object would give: Jane => Doe, John => Doe, | |
for(var key in names) { | |
if (names.hasOwnProperty(key)) { | |
console.log(key, '=>', names[key]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment