getOwnPropertyNames【プロパティ名 取得】
getOwnPropertySymbols【シンボル プロパティ 取得】
getOwnPropertyDescriptor【プロパティ記述子 取得】
getOwnPropertyDescriptors【全プロパティ記述子 取得】
hasOwnProperty【プロパティ有無】
hasOwn【プロパティ有無 (省略形)】
Object.getOwnPropertyNames【プロパティ名 取得】
Object.getOwnPropertySymbols【シンボル プロパティ 取得】
メモ
概要
- プロパティ名・シンボル プロパティ を配列で取得
関連
外部リンク
- ECMA-262 (英語)
Object.getOwnPropertyNames ( O ) ES2024 (15) ES2023 (14) ES2022 (13) Object.getOwnPropertySymbols ( O ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
Object.getOwnPropertyNames( O )
プロパティ名の配列 (列挙の可否は無関係・シンボル使用は対象外)
O オブジェクト (オブジェクト以外はオブジェクト変換 )
TypeError 例外
O (オブジェクト)がオブジェクト以外
O (オブジェクト)がオブジェクト変換不可
Object.getOwnPropertySymbols( O )
シンボル プロパティの配列 (列挙の可否は無関係)
O オブジェクト (オブジェクト以外はオブジェクト変換)
TypeError 例外 O (オブジェクト)がオブジェクト変換不可
例
const obj = { prop1: 'Value1' };
obj.prop2 = 'Value2';
Object.defineProperty(obj, 'propNonenum', { value: 'ValueUnenum', enumerable: false });
console.log(Object.getOwnPropertyDescriptor(obj, 'propNonenum'));
// 出力:Object { value: "ValueUnenum", writable: false, enumerable: false, configurable: false }
const symbolLocal = Symbol('PropSymbolLocal');
obj[symbolLocal] = 'ValueSymbolLocal';
const symbolGlobal = Symbol.for('PropSymbolGlobal');
obj[symbolGlobal] = 'ValueSymbolGlobal';
const symbolNonenum = Symbol('PropSymbolNonenum');
Object.defineProperty(obj, symbolNonenum, { value: 'ValueSymbolGlobal', enumerable: false });
console.log(Object.getOwnPropertyDescriptor(obj, symbolNonenum));
// 出力:Object { value: "ValueSymbolGlobal", writable: false, enumerable: false, configurable: false }
// getOwnPropertyNames
console.log(Object.getOwnPropertyNames(obj));
// 出力:Array(3) [ "prop1", "prop2", "propNonenum" ]
// keys (参考)
console.log(Object.keys(obj));
// 出力:Array [ "prop1", "prop2" ]
// for-in (参考)
for (const key in obj) {
console.log(key);
}
// 出力:
// prop1
// prop2
// getOwnPropertySymbols
console.log(Object.getOwnPropertySymbols(obj));
// 出力:Array(3) [ Symbol("PropSymbolLocal"), Symbol("PropSymbolGlobal"), Symbol("PropSymbolNonenum") ]
Object.getOwnPropertyDescriptor【プロパティ記述子 取得】
Object.getOwnPropertyDescriptors【全プロパティ記述子 取得】
メモ
概要
- プロパティ記述子を取得
関連
外部リンク
- ECMA-262 (英語)
Object.getOwnPropertyDescriptor ( O, P ) ES2024 (15) ES2023 (14) ES2022 (13) Object.getOwnPropertyDescriptors ( O ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
Object.getOwnPropertyDescriptor( O, P )
プロパティ記述子 (undefined:該当プロパティなし)
O オブジェクト (オブジェクト以外はオブジェクト変換 )
P プロパティ名・Symbol【シンボル】
TypeError 例外
O (オブジェクト)がオブジェクト以外
O (オブジェクト)がオブジェクト変換不可
Object.getOwnPropertyDescriptors( O )
プロパティ記述子のオブジェクト (空オブジェクト:プロパティなし)
キー: プロパティ名 または Symbol【シンボル】
値: 属性の組合せ
O オブジェクト (オブジェクト以外はオブジェクト変換 )
TypeError 例外 O (オブジェクト)がオブジェクト変換不可
例
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
Object.prototype.hasOwnProperty【プロパティ有無】
Object.hasOwn【プロパティ有無 (省略形)】
メモ
概要
- 指定プロパティの有無の判定
外部リンク
- ECMA-262 (英語)
Object.prototype.hasOwnProperty ( V ) ES2024 (15) ES2023 (14) ES2022 (13) Object.hasOwn ( O, P ) ES2024 (15) ES2023 (14) ES2022 (13)
構文
Object.hasOwnProperty( V )
Object.prototype.hasOwnProperty.call(O, V) ※
プロパティ有無
V プロパティ名・Symbol【シンボル】
O オブジェクト
※:同名メソッドの上書きを考慮すると2番目の構文
hasOwn【プロパティ有無 (省略形)】 は省略形
Object.hasOwn( O, P )
プロパティ有無
O オブジェクト (オブジェクト以外はオブジェクト変換)
P プロパティ名・Symbol【シンボル】
例
const obj = {
prop1: 'Value1',
hasOwnProperty: function() { return false; }
};
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' });
console.log(Object.getOwnPropertyNames(obj));
// 出力:Array(4) [ "prop1", "hasOwnProperty", "prop2", "propUnenum" ]
console.log(Object.getOwnPropertySymbols(obj));
// 出力:Array [ Symbol("PropSymbolLocal"), Symbol("PropSymbolGlobal") ]
// hasOwnProperty (構文1)
console.log(obj.hasOwnProperty('prop1'));
// 出力:false
console.log(obj.hasOwnProperty('propUnenum'));
// 出力:false
// hasOwnProperty (構文2)
console.log(Object.prototype.hasOwnProperty.call(obj, 'prop1'));
// 出力:true
console.log(Object.prototype.hasOwnProperty.call(obj, 'prop2'));
// 出力:true
console.log(Object.prototype.hasOwnProperty.call(obj, symbolLocal));
// 出力:true
console.log(Object.prototype.hasOwnProperty.call(obj, symbolGlobal));
// 出力:true
console.log(Object.prototype.hasOwnProperty.call(obj, 'propUnenum'));
// 出力:true
console.log(Object.prototype.hasOwnProperty.call(obj, 'propX'));
// 出力:false
// hasOwn
console.log(Object.hasOwn(obj, 'prop1'));
// 出力:true
console.log(Object.hasOwn(obj, 'prop2'));
// 出力:true
console.log(Object.hasOwn(obj, symbolLocal));
// 出力:true
console.log(Object.hasOwn(obj, symbolGlobal));
// 出力:true
console.log(Object.hasOwn(obj, 'propUnenum'));
// 出力:true
console.log(Object.hasOwn(obj, 'propX'));
// 出力:false