1.定义parent对象及其属性
var parent = {}
Object.defineProperty(parent, "a", {
value: "aaa",
writable: true,
enumerable: true,
configurable: true
});
Object.defineProperty(parent, "b", {
value: "bbb",
writable: true,
enumerable: false,
configurable: true
});
parent.c = Symbol('ccc');
var m = Symbol('mmm');
parent[m] = m;
console.log(parent);
2.定义child对象及其属性
var child = {}
Object.defineProperties(child, {
d: {
value: "ddd",
writable: true,
enumerable: true,
configurable: true
},
"e": {
value: "eee",
writable: true,
enumerable: false,
configurable: true
}
})
child.f = Symbol('fff');
var n = Symbol('nnn');
child[n] = n;
console.log(child);
3.让child继承parent
child.__proto__ = parent;
4. 区别
①for in 循环遍历对象自身的和继承的可枚举属性(不含Symbol属性)。
for(var key in child){
console.log(key)
}
②Object.keys 返回一个数组,包括对象自身的(不含继承的)所有可枚举属性(不含Symbol属性)。
console.log(Object.keys(child))
③Object.getOwnPropertyNames 返回一个数组,包含对象自身的所有属性(不含Symbol属性,但是包括不可枚举属性)。
console.log(Object.getOwnPropertyNames(child))
④Object.getOwnPropertySymbols 返回一个数组,包含对象自身的所有Symbol属性。
console.log(Object.getOwnPropertySymbols(child))
⑤Reflect.ownKeys 返回一个数组,包含对象自身的所有属性,不管是属性名是Symbol或字符串,也不管是否可枚举
console.log(Reflect.ownKeys(child))