const log = console.log;
function Animal() {
this.aniName = "name animal";
Object.defineProperties(this, {
"key1": { value: "value1", enumerable: true },
"key2": { value: "value2", enumerable: false },
[Symbol.for("symkey1")]: { value: "symbvalue1", enumerable: true },
[Symbol.for("symkey2")]: { value: "symbvalue2", enumerable: false }
});
}
Animal.prototype.pinfoAnimal = function() {
return `i am ${this.aniName}`;
}
function Cat() {
Animal.call(this);
this.catName = "name cat";
}
Cat.prototype = Object.create(Animal.prototype);
Cat.prototype.pinfoCat = function() {
return `i am ${this.catName}`;
}
Cat.prototype.constructor = Cat;
function RedCat() {
Cat.call(this);
this.redCatName = "name red cat";
}
RedCat.prototype = Object.create(Cat.prototype);
RedCat.prototype.constructor = RedCat;
let rc = new RedCat();
log("rc = ", rc);
log("RedCat = ", RedCat);
for (let k in rc) {
log("in k = ", k, ", v = ", rc[k]);
}
log("rc.getOwnPropertyDescriptors = ", Object.getOwnPropertyDescriptors(rc));
log("getOwnPropertyNames = ", Object.getOwnPropertyNames(rc));
log("getOwnPropertySymbols = ", Object.getOwnPropertySymbols(rc));
log("entries = ", Object.entries(rc));
for (let [k, v] of Object.entries(rc)) {
log("entries k = ", k, ", v = ", v);
}
log("getPrototypeOf = ", Object.getPrototypeOf(rc));
let old = { "k1": "v1" };
old.run = function() {
return `old-k1: ${this.k1}`;
}
let n = Object.create(old);
log("old = ", old);
log("n = ", n);
for (let k in n) {
log("n k=", k, ", v = ", n[k]);
}
log("old.getOwnPropertyDescriptors = ", Object.getOwnPropertyDescriptors(old));
log("n.getOwnPropertyDescriptors = ", Object.getOwnPropertyDescriptors(n));
for (let k in n) {
log("n.getOwnPropertyDescriptor(", k, ")=", Object.getOwnPropertyDescriptor(n, k));
}
结果:
$ node for.js
rc = RedCat {
aniName: 'name animal',
key1: 'value1',
catName: 'name cat',
redCatName: 'name red cat',
[Symbol(symkey1)]: 'symbvalue1'
}
RedCat = [Function: RedCat]
in k = aniName , v = name animal
in k = key1 , v = value1
in k = catName , v = name cat
in k = redCatName , v = name red cat
in k = constructor , v = [Function: RedCat]
in k = pinfoCat , v = [Function]
in k = pinfoAnimal , v = [Function]
rc.getOwnPropertyDescriptors = {
aniName: {
value: 'name animal',
writable: true,
enumerable: true,
configurable: true
},
key1: {
value: 'value1',
writable: false,
enumerable: true,
configurable: false
},
key2: {
value: 'value2',
writable: false,
enumerable: false,
configurable: false
},
catName: {
value: 'name cat',
writable: true,
enumerable: true,
configurable: true
},
redCatName: {
value: 'name red cat',
writable: true,
enumerable: true,
configurable: true
},
[Symbol(symkey1)]: {
value: 'symbvalue1',
writable: false,
enumerable: true,
configurable: false
},
[Symbol(symkey2)]: {
value: 'symbvalue2',
writable: false,
enumerable: false,
configurable: false
}
}
getOwnPropertyNames = [ 'aniName', 'key1', 'key2', 'catName', 'redCatName' ]
getOwnPropertySymbols = [ Symbol(symkey1), Symbol(symkey2) ]
entries = [
[ 'aniName', 'name animal' ],
[ 'key1', 'value1' ],
[ 'catName', 'name cat' ],
[ 'redCatName', 'name red cat' ]
]
entries k = aniName , v = name animal
entries k = key1 , v = value1
entries k = catName , v = name cat
entries k = redCatName , v = name red cat
getPrototypeOf = RedCat { constructor: [Function: RedCat] }
old = { k1: 'v1', run: [Function] }
n = {}
n k= k1 , v = v1
n k= run , v = [Function]
old.getOwnPropertyDescriptors = {
k1: { value: 'v1', writable: true, enumerable: true, configurable: true },
run: {
value: [Function],
writable: true,
enumerable: true,
configurable: true
}
}
n.getOwnPropertyDescriptors = {}
n.getOwnPropertyDescriptor( k1 )= undefined
n.getOwnPropertyDescriptor( run )= undefined