in 与 hasOwnProperty的区别

inhasOwnProperty的区别javascript

  • 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
相关文章
相关标签/搜索