📜  JavaScript 不保护属性名称 hasOwnProperty - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:01:22.631000             🧑  作者: Mango

代码示例1
// 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