in
与 hasOwnProperty
的区别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