📜  hasOwnProperty - Javascript 代码示例

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

代码示例6
//
//2021 - Object.hasOwn as a replacement for Object.hasOwnProperty()

//As other answers indicated, hasOwnProperty will check for an object own properties in contrast to in which will also check for inherited properties.

//There is a new alternative method called Object.hasOwn() and is intended to be a replacement for Object.hasOwnProperty()**

const person = { name: 'dan' };

console.log(Object.hasOwn(person, 'name'));// true
console.log(Object.hasOwn(person, 'age'));// false

const person2 = Object.create({gender: 'male'});

console.log(Object.hasOwn(person2, 'gender'));// false
 Run code snippet