Answers for "JavaScript does not protect the property name hasOwnProperty"

1

JavaScript does not protect the property name hasOwnProperty

// hasOwnProperty – JavaScript does not protect
var foo = {
    // overriding foo's default hasOwnProperty method
    hasOwnProperty: function() {
        return false;
    },
    bar: 'data'
};
foo.hasOwnProperty('bar'); // false always

// Hence, to prevent this, use Object.prototype.hasOwnProperty as follows-
Object.prototype.hasOwnProperty.call(foo, 'bar'); // true
Posted by: Guest on July-04-2021

Code answers related to "JavaScript does not protect the property name hasOwnProperty"

Code answers related to "Javascript"

Browse Popular Code Answers by Language