js对象中的可枚举属性与不可枚举属性的区别?

不能枚举即 使用for...in 不能遍历到
是不是意味着对象copy的时候不可枚举属性就不会被复制出来?

阅读 8.4k
2 个回答

propertyIsEnumerable()方法可以用来判断这个属性是否可枚举

var person = {
    'name': 'ding'
};
console.log('name' in person);  // true
console.log(person.propertyIsEnumerable('name'));  // true

console.log('length' in person);  // true
console.log(person.propertyIsEnumerable('length')); // false

name是person的自有属性可枚举,length是原型属性不可枚举

不可枚举就是for-in打印不出来的吧,Object.defineProperty(),试试。

推荐问题