DEV Community

Discussion on: hasOwnProperty considered harmful

Collapse
 
joshualjohnson profile image
Joshua Johnson • Edited

Sorry man, I'm still not understanding the message you are trying to convey. Is it that when looping through object literals that don't inherit from other objects, it's not necessary to include the hasOwnProperty check?

var prop;
var A = {
    foo: 'bar',
    fooFn: function () {}
};

var B = Object.create(a);


/**
 * ensures that you are only looping through
 * properties that belong only to B. Will not
 * execute code on properties from A.
 */
for (prop in B) {
    if (b.hasOwnProperty(prop)) {
        console.log(b[prop]);
    }
}

/**
 * will execute code on not only properties on B
 * but it will also loop through properties on A.
 */
for (prop in B) {
    console.log(b[prop]);
}