相同点:
Object.hasOwnProperty和Object.propertyIsEnumerable都是用来判断目标属性是否为该对象的属性(不包括从原型链上继承的属性)
目标对象:object 目标属性:targetAttribute
不同点:
1.hasOwnProperty遍历的是目标对象自身可遍历的属性
用法:object.hasOwnProperty(targetAttribute) 判断属性是否存在于当前对象上,返回一个boolean类型值,存在为true,否则false
2.propertyIsEnumerable遍历的是目标对象所有可遍历并且可枚举的属性
用法:object.propertyIsEnumerable(targetAttribute) 判断属性是否存在于当前对象上并且可枚举,返回一个boolean类型值,存在为true,否则false
例子:
var a = {};
Object.defineProperties(a, {
'p1': {
value: 12,
enumerable: false
},
'p3': {
value: 33,
enumerable: true
}
})
console.log(a.hasOwnProperty('p1')); // true
console.log(a.propertyIsEnumerable('p1')); // false
由于属性p1设置了不可枚举属性,两者检测便返回不同结果