in
与 hasOwnProperty
的区别
in
不仅会查找对象实例自身属性,还会查找其原型属性hasOwnProperty
只会查找对象实例自身属性
let obj = {
name: 'ys'}
Object.setPrototypeOf(obj,{
action:'move'})
console.log('name' in obj) // true
console.log('action' in obj) // true
console.log(obj.hasOwnProperty('name'))//true
console.log(obj.hasOwnProperty('action'))//false