1.in 运算符方法

对象的自有属性或继承属性中包含这个属性则返回true;

var obj = {
    name:'david'
};
console.log('name' in obj); // --> true
console.log('toString' in obj); // --> true

无论是key值name,还是原形链上的toString,都能检测到返回true。

2.hasOwnProperty 方法

用来检测是否是对象自有属性,对于继承属性返回false;

var obj = {
    name:'david'
};
obj.hasOwnProperty('name'); // --> true
obj.hasOwnProperty('toString'); // --> false

原型链上继承过来的属性toString无法通过hasOwnProperty检测到,返回false。

3.propertyIsEnumerable方法

hasOwnProperty的加强版,只有属性是自有而且是可枚举的才返回true;

相关文章:

  • 2021-09-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-31
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案