Skip to content

Instantly share code, notes, and snippets.

@bodbdigr
Created April 12, 2011 14:08
Show Gist options
  • Save bodbdigr/915548 to your computer and use it in GitHub Desktop.
Save bodbdigr/915548 to your computer and use it in GitHub Desktop.
//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