const obj = {};
Reflect.defineProperty(obj, "key1", {
configurable: true,
enumerable: true,
value: 1,
writable: false,
});
Reflect.defineProperty(obj, Symbol("key1"), {
configurable: true,
enumerable: true,
value: 1,
writable: false,
});
Reflect.defineProperty(obj, "key2", {
configurable: true,
enumerable: false,
value: 1,
writable: false,
});
Reflect.defineProperty(obj, Symbol("key2"), {
configurable: true,
enumerable: false,
value: 1,
writable: false,
});
Reflect.setPrototypeOf(obj, {
prototypeKey: 1
});
console.log(Object.getOwnPropertyNames(obj));
console.log(Object.getOwnPropertySymbols(obj));
console.log(Object.keys(obj));
for (const key in obj) {
console.log(key);
}
Object.getOwnPropertyNames(obj):返回非 Symbol 的 Keys
Object.getOwnPropertySymbols(obj):返回 Symbol 的 Keys
Object.keys(obj):返回属性描述符 enumerable 为 true 的 非 Symbol 的 Keys
for..in:返回属性描述符 enumerable 为 true 的 非 Symbol 的 Keys, 原型链上原型的属性也如此循环查找