js 面向对象之属性描述符

上回介绍了面向对象之构造器属性。这次介绍下属性描述符

遍历对象属性

let person = {name: "lisi"}
for (key in person) {
    console.log(key)
}

Object.keys(person)

 

属性描述

let person = {name: "lisi"}
for (key in person) {
    console.log(key)
}

Object.keys(person)

let objectBase = Object.getPrototypeOf(person)

let descriptor = Object.getOwnPropertyDescriptor(objectBase, "toString")
console.log(descriptor)

//output:
{
    value: [Function: toString],
    writable: true, // 是否只读
    enumerable: false, //是否可遍历
    configurable: true // 是否可删除
}

 

自定义描述符

Object.defineProperty(person, 'name', {
    writable: false,
    enumerable: false,
    configurable: false
})

 

posted on 2019-11-14 13:56  Lemo_wd  阅读(245)  评论(0编辑  收藏  举报

导航