const obj = { prop1: 'Value1' };
obj.prop2 = 'Value2';
const symbolLocal = Symbol ('PropSymbolLocal');
obj[symbolLocal] = 'ValueSymbolLocal';
const symbolGlobal = Symbol.for ('PropSymbolGlobal');
obj[symbolGlobal] = 'ValueSymbolGlobal';
Object.defineProperty (obj, 'propUnenum', { enumerable: false, value: 'ValueUnenum' });
// Object.getOwnPropertyNames (参考)
console.log (Object.getOwnPropertyNames (obj));
// 出力:
// Object.getOwnPropertySymbols (参考)
console.log (Object.getOwnPropertySymbols (obj));
// 出力:Array [ Symbol("PropSymbolLocal"), Symbol("PropSymbolGlobal") ]
// Object.getOwnPropertyDescriptor
const desc1 = Object.getOwnPropertyDescriptor (obj, 'prop1') ;
console.log (desc1);
// 出力:Object { value: "Value1", writable: true, enumerable: true, configurable: true }
console.log (desc1.value, desc1.enumerable);
// 出力:Value1 true
const desc2 = Object.getOwnPropertyDescriptor (obj, 'prop2') ;
console.log (desc2);
// 出力:Object { value: "Value2", writable: true, enumerable: true, configurable: true }
console.log (desc2.value, desc2.enumerable);
// 出力:Value2 true
const descUnenum = Object.getOwnPropertyDescriptor (obj, 'propUnenum') ;
console.log (descUnenum);
// 出力:Object { value: "ValueUnenum", writable: false, enumerable: false, configurable: false }
console.log (descUnenum.value, descUnenum.enumerable);
// 出力:ValueUnenum false
const descSymbolLocal = Object.getOwnPropertyDescriptor (obj, symbolLocal) ;
console.log (descSymbolLocal);
// 出力:Object { value: "ValueSymbolLocal", writable: true, enumerable: true, configurable: true }
console.log (descSymbolLocal.value, descSymbolLocal.enumerable);
// 出力:ValueSymbolLocal true
const descSymbolGlobal = Object.getOwnPropertyDescriptor (obj, symbolGlobal) ;
console.log (descSymbolGlobal);
// 出力:Object { value: "ValueSymbolGlobal", writable: true, enumerable: true, configurable: true }
console.log (descSymbolGlobal.value, descSymbolGlobal.enumerable);
// 出力:ValueSymbolGlobal true
console.log (Object.getOwnPropertyDescriptor (obj, 'propXYZ') );
// 出力:undefined
// Object.getOwnPropertyDescriptors
const desc = Object.getOwnPropertyDescriptors (obj) ;
console.log (desc);
// 出力:
// Object { prop1: {…}, prop2: {…}, propUnenum: {…}, Symbol("PropSymbolLocal"): {…}, Symbol("PropSymbolGlobal"): {…} }
// prop1: Object { value: "Value1", writable: true, enumerable: true, … }
// prop2: Object { value: "Value2", writable: true, enumerable: true, … }
// propUnenum: Object { value: "ValueUnenum", writable: false, enumerable: false, … }
// Symbol(PropSymbolLocal): Object { value: "ValueSymbolLocal", writable: true, enumerable: true, … }
// Symbol(PropSymbolGlobal): Object { value: "ValueSymbolGlobal", writable: true, enumerable: true, … }
console.log (desc.prop1.value, desc.prop1.enumerable);
// 出力:Value1 true
console.log (desc.prop2.value, desc.prop2.enumerable);
// 出力:Value2 true
console.log (desc.propUnenum.value, desc.propUnenum.enumerable);
// 出力:ValueUnenum false
console.log (desc[symbolLocal].value, desc[symbolLocal].enumerable);
// 出力:ValueSymbolLocal true
console.log (desc[symbolGlobal].value, desc[symbolGlobal].enumerable);
// 出力:ValueSymbolGlobal true