本文主要是介绍Object.hasOwnProperty和Object.propertyIsEnumerable的区别,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
相同点:
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设置了不可枚举属性,两者检测便返回不同结果
这篇关于Object.hasOwnProperty和Object.propertyIsEnumerable的区别的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!