Object.hasOwnProperty() in JavaScript

On StackOverflow, user Pablo Cabrera points out:

As slashnick pointed out, you can use the “for in” construct to iterate over an object for its attribute names. However you’ll be iterating over all attribute names in the object’s prototype chain. If you want to iterate only over the object’s own attributes, you can make use of the Object#hasOwnProperty() method. Thus having the following.

for (var key in obj) {
if (obj.hasOwnProperty(key)) {
/* useful code here */
}
}

Wish I’d known of that a year ago.

1 thought on “Object.hasOwnProperty() in JavaScript”

Comments are closed.