为什么对象的有些属性在 Chrome 控制台中显示是淡色的

在这里插入图片描述
淡色属性代表不可枚举!注意不可枚举的属性会影响 for ... inObject.keys()JSON.stringify() 等的结果!

function Person() {
    
    
    this.name = 'ifer';
}
Person.prototype.age = 18;

const p = new Person();

Object.defineProperty(p, 'sex', {
    
    
    value: 'man', // 内容
    writable: true, // 修改
    configurable: true, // 删除
    enumerable: false, // 枚举
});

console.log(p);

Object.getOwnPropertyDescriptor(p, 'name'); // 获取属性修饰符

猜你喜欢

转载自blog.csdn.net/dangpugui/article/details/114557423