DEV Community

Discussion on: hasOwnProperty considered harmful

Collapse
 
joshualjohnson profile image
Joshua Johnson

hasOwnPropery is meant as a way to ensure that you are only iterating over values that belong to the object you are checking the property against. Meaning that you won't use cpu to process information on properties that live on the parent of the object you inherited from.

Also, I'm not sure what you mean by "hijacked"? I'm not sure how that plays into your argument about the object growing or not to use hasOwnPropery?

Collapse
 
Sloan, the sloth mascot
Comment deleted
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]);
}