跳到主要内容

测试属性

测试属性:判断相关属性是否在对象中

in 操作符

是自己的属性,也可以是继承来的属性
let o = { x: 1 }
'x' in o // =>true o对象中有自有属性“x”
'y' in o // =>false o 没有属性 “y”
'toString' in o // true :o 继承了toString属性

hasOwnProperty()

  • has 有
  • Own 自己的
  • Property 特性
必须是自己的属性,不能是继承的
let o = { x: 1 }
o.hasOwnProperty('x') // => true: o 有自有属性
o.hasOwnProperty('y') // => false: o 没有属性y
o.hasOwnProperty('toString') // =>false: toString 是继承属性

propertyIsEnumerable()

  • property 特性
  • Is 是
  • Enumerable 可以枚举的
是自己的属性,并且这个属性的enumerable(可枚举)特性为true
let o = { x: 1 }
o.propertyIsEnumerable('x') // => true
o.propertyIsEnumerable('toString') // => false 不可以枚举