in 与 hasOwnProperty的区别

inhasOwnProperty的区别

  • 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

猜你喜欢

转载自blog.csdn.net/m0_37285193/article/details/118941145