【1】正常使用 hasOwnProperty()
判断对象中是否有某个属性
let o = {
name: '张三'
}
let result = o.hasOwnProperty('name')
console.log('result---', result)
【2】对象中有 hasOwnProperty 属性时,使用 hasOwnProperty()
判断对象中是否有某个属性
let oo = {
hasOwnProperty: '789',
name: '李四'
}
let ooo = {
hasOwnProperty: function () {
return '嘿嘿嘿'
},
name: '李四'
}
console.log('ooo.hasOwnProperty("name")----', ooo.hasOwnProperty('name'))
console.log('ooo.hasOwnProperty("hasOwnProperty")----', ooo.hasOwnProperty('hasOwnProperty'))
【注意】
let obj = {
hasOwnProperty: function () {
return '哈哈哈'
},
name: '李四'
}
console.log(
'Object.hasOwnProperty.call(obj, "name")----',
Object.hasOwnProperty.call(obj, 'name')
)
console.log(
'Object.hasOwnProperty.call(obj, "hasOwnProperty")----',
Object.hasOwnProperty.call(obj, 'hasOwnProperty')
)
MDN 地址 Object.prototype.hasOwnProperty()