直接看结果
const log = console.log;
let ksymbol = Symbol.for("iamsymbol")
let ksym2 = Symbol.for("ksym2kk")
let k1 = "iamk1"
let k2 = "iamk2"
let k3 = "iamk3"
let obj = {
[k1]: "v1",
[k2]: "v2",
[ksymbol]: "vsym"
};
let no = Object.create(obj, {
[k3]: {
value: "v3" //默认writable、enumerable、configurable都为false
},
[ksym2]: {
value: "vsym2",
writable: true,
enumerable: true,
configurable: true
}
})
for (let k in no) {
log("k ", k, ", v = ", no[k])
}
log("desc obj ksymbol = ", Object.getOwnPropertyDescriptor(obj, ksymbol))
log("desc no ksymbol = ", Object.getOwnPropertyDescriptor(no, ksymbol))//undefined
log("desc k3 = ", Object.getOwnPropertyDescriptor(no, k3), ", v3 = ", no[k3])
log("Object.getOwnPropertyNames(no) = ", Object.getOwnPropertyNames(no));//只能获得this中的不能获得__proto__中的,symbol的也不显示
log("Object.getOwnPropertySymbols(no) = ", Object.getOwnPropertySymbols(no));//只能获得this中的,不获得__proto__中的
log("Object.getOwnPropertySymbols(obj) = ", Object.getOwnPropertySymbols(obj));
/**
*
$ node src/entry.js
k iamk1 , v = v1
k iamk2 , v = v2
desc obj ksymbol = { value: 'vsym', writable: true, enumerable: true, configurable: true }
desc no ksymbol = undefined
desc k3 = {
value: 'v3',
writable: false,
enumerable: false,
configurable: false
} , v3 = v3
Object.getOwnPropertyNames(no) = [ 'iamk3' ]
Object.getOwnPropertySymbols(no) = [ Symbol(ksym2kk) ]
Object.getOwnPropertySymbols(obj) = [ Symbol(iamsymbol) ]
*/