Object.hasOwn 替换 in有时,我们想知道一个属性是否存在于一个对象中,我们会使用in操作符或者obj.hasOwnProperty。这两种方法有缺陷
in操作符
in操作符会返回true当一个属性在一个对象上或者它的原型链上
const Person = function (age) {this.age = age}Person.prototype.name = 'fatfish'const p1 = new Person(24)console.log('age' in p1) // trueconsole.log('name' in p1) // true pay attention here
obj.hasOwnProperty