Object【オブジェクト】 の連想配列 | Map【マップ】 | Set 【一意コレクション】 | WeakMap 【弱参照マップ】 | WeakSet 【弱参照一意コレクション】 | |
---|---|---|---|---|---|
コンストラクタ | オブジェクトリテラル new Object ( [ value ] ) | new Map ( [ iterable ] ) | new Set ( [ iterable ] ) | new WeakMap ( [ iterable ] ) | new WeakSet ( [ iterable ] ) |
キー | 文字列・Symbol | 任意 | オブジェクトのみ | ||
値 | 任意 | 任意 | 任意 | 任意 | オブジェクトのみ |
ガベージ手動 | 手動 | 自動 | | ||
要素数 | Object.keys ( obj ).length | size プロパティ | size プロパティ | ||
要素追加・更新 | obj.key obj [ 'key' ] セッター | set ( key, value ) | add ( value ) | set ( key, value ) | add ( value ) |
要素有無 | hasOwnProperty(V) | has ( key ) | has ( value ) | has ( key ) | has ( value ) |
要素値取得 | obj.key obj [ 'key' ] ゲッター | get ( key ) | get ( key ) | ||
要素削除 | delete obj.key delete obj [ 'key' ] | delete ( key ) | delete ( value ) | delete ( key ) | delete ( value ) |
全要素削除 | clear ( ) | clear ( ) | |||
イテレータ オブジェクト作成 (キー・値) | Object.entries(O) (配列取得) | [@@iterator] ( ) entries ( ) | entries ( ) (キー = 値) | ||
イテレータ オブジェクト作成 (キー) | Object.keys( O ) (配列取得) | keys ( ) | keys ( ) (キー = 値) | ||
イテレータ オブジェクト作成 (値) | Object.values(O) (配列取得) | values ( ) | [@@iterator] ( ) values ( ) | ||
反復要素処理 | for-in | for-of forEach (callbackfn [, thisArg]) next() | for-of forEach (callbackfn [, thisArg]) next() |
ECMAScript (英語)
The Object Type | ||
---|---|---|
ES2022 (13) | ES2021 (12) | ES2020 (11) |
Object Objects | ||
ES2022 (13) | ES2021 (12) | ES2020 (11) |
Property Accessors | ||
ES2022 (13) | ES2021 (12) | ES2020 (11) |
Object Initializer | ||
ES2022 (13) | ES2021 (12) | ES2020 (11) |
Additional Properties of the Object.prototype Object | ||
ES2022 (13) | ES2021 (12) | ES2020 (11) |
Other Additional Features | ||
ES2022 (13) | ES2021 (12) | ES2020 (11) |
構文 | 説明 |
---|---|
オブジェクトリテラル new Object ( [ value ] ) | コンストラクタ |
プロパティ | 説明 | |
---|---|---|
Object.prototype. | __proto__ | 内部プロトタイプ |
Object.prototype. | constructor | コンストラクタ定義 |
Object. | prototype | プロトタイプ |
参考 | 説明 | |
Object.prototype | [ @@toStringTag ] | タグ (ユーザ定義の判別可能) toString【文字列変換】で使用 |
メソッド | 説明 | |
---|---|---|
Object. | assign ( target, ...sources ) | プロパティ コピー |
Object. | create ( O [, Properties] ) | 作成 |
Object. | defineProperties ( O, Properties ) | プロパティ定義 (複数) |
Object. | defineProperty ( O, P, Attributes ) | プロパティ定義 (単一) |
Object. | entries ( O ) | プロパティ配列取得 (キー・値) |
Object. | freeze ( O ) | プロパティ凍結 |
Object. | fromEntries ( iterable ) | オブジェクト生成 (イテラブル) |
Object. | getOwnPropertyDescriptor ( O, P ) | プロパティ記述子 取得 |
Object. | getOwnPropertyDescriptors ( O ) | 全プロパティ記述子 取得 |
Object. | getOwnPropertyNames ( O ) | プロパティ名 取得 |
Object. | getOwnPropertySymbols ( O ) | シンボル プロパティ 取得 |
Object. | getPrototypeOf ( O ) | プロトタイプ 取得 |
Object.prototype. | hasOwn ( O, P ) | プロパティ有無 (省略形) |
Object.prototype. | hasOwnProperty ( V ) | プロパティ有無 |
Object. | is ( value1, value2 ) | 同一判定 |
Object. | isExtensible ( O ) | 拡張(プロパティ追加)可否 |
Object. | isFrozen ( O ) | プロパティ凍結 判定 |
Object.prototype. | isPrototypeOf ( V ) | プロトタイプチェーン存在有無 |
Object. | isSealed ( O ) | 封印 判定 |
Object. | keys (O) | プロパティ配列取得 (キー) |
Object. | preventExtensions (O) | 拡張(プロパティ追加)不可 設定 |
Object.prototype. | propertyIsEnumerable (V) | 列挙可能プロパティ 判定 |
Object. | seal ( O ) | 封印 |
Object. | setPrototypeOf ( O, proto ) | プロトタイプ設定 |
Object.prototype. | toLocaleString () toLocaleString ( [ reserved1 [ , reserved2 ] ] ) | 文字列変換 (ロケール) |
Object.prototype. | toString () | 文字列変換 ([ @@toStringTag ] プロパティ 参照) |
Object.prototype. | unwatch ( prop ) | 代入監視終了 |
Object.prototype. | valueOf () | プリミティブ値 変換 |
Object. | values ( O ) | プロパティ配列取得 (値) |
Object.prototype. | watch ( prop, handler ) | 代入監視開始 |
プロパティ定義 | 例 | 説明 |
---|---|---|
CoverInitializedName | var xyz = 'xyz'; var obj = { xyz }; | CoverInitializedName:変数名 プロパティ名:変数名 プロパティ値:変数値 |
PropName : Expression | ・xyz : "xyz" ・func : function (~) { ~ } | PropName:プロパティ名 (文字列に変換) Expression:プロパティ値 (式) NumericLiteral:数値 (文字列に変換) [PropName]:文字列で記述・式も可能 |
"PropName" : Expression | "xyz" : "xyz" | |
NumericLiteral : Expression | 123 : "xyz" | |
[PropName] : Expression | ・[xyz] : "xyz" ・["xy" + "z"] : "xyz" | |
PropName ( Parameters ) { FuncBody } | func (~) { ~ } | 関数定義の省略形 Parameters:パラメータ FuncBody:処理 |
GeneratorMethod | ジェネレータ | |
get PropName () { FuncBody } | get xValue () { return this.x; } | ゲッター FuncBody:処理 |
set PropName ( Parameters ) { FuncBody } | set xValue (value) { this.x = value; } | セッター Parameters:パラメータ FuncBody:処理 |
設定 判定 | プロパティ操作 | strict モード エラー | ||
---|---|---|---|---|
変更 | 追加 | 削除 | ||
seal【封印】 isSealed【封印 判定】 | ○ | × | × | TypeError 例外 |
freeze【プロパティ凍結】 isFrozen【プロパティ凍結 判定】 | × | × | × | TypeError 例外 |
preventExtensions【拡張(プロパティ追加)不可 設定】 isExtensible【拡張(プロパティ追加)可否】 | ○ | × | ○ | TypeError 例外 |
const obj = { propA: 'ValueA', propB: 'ValueB', propC: 'ValueC' };
console.log(obj.propA, obj.propB, obj.propC);
// 出力:ValueA ValueB ValueC
console.log(obj['propA'], obj['propB'], obj['propC']);
// 出力:ValueA ValueB ValueC
console.log(Object.keys(obj));
// 出力:Array(3) [ "propA", "propB", "propC" ]
console.log(Object.values(obj));
// 出力:Array(3) [ "ValueA", "ValueB", "ValueC" ]
console.log(Object.entries(obj));
// 出力:
// Array(3) [ (2) […], (2) […], (2) […] ]
// 0: Array [ "propA", "ValueA" ]
// 1: Array [ "propB", "ValueB" ]
// 2: Array [ "propC", "ValueC" ]
// length: 3
for (const key in obj) {
console.log(key);
}
// 出力:
// propA
// propB
// propC
let toString = Object.prototype.toString;
let global = null;
console.log(typeof global); // 出力:object
console.log(toString.call(global)); // 出力:[object Null]
global = undefined;
console.log(typeof global); // 出力:undefined
console.log(toString.call(global)); // 出力:[object Undefined]
global = NaN;
console.log(typeof global); // 出力:number
console.log(toString.call(global)); // 出力:[object Number]
global = Infinity;
console.log(typeof global); // 出力:number
console.log(toString.call(global)); // 出力:[object Number]
let bool1 = true;
let bool2 = new Boolean(true);
console.log(typeof bool1); // 出力:boolean
console.log(typeof bool2); // 出力:object
console.log(toString.call(bool1)); // 出力:[object Boolean]
console.log(toString.call(bool2)); // 出力:[object Boolean]
let array1 = [ 1, 2, 3];
let array2 = new Array([ 1, 2, 3]);
console.log(typeof array1); // 出力:object
console.log(typeof array2); // 出力:object
console.log(toString.call(array1)); // 出力:[object Array]
console.log(toString.call(array2)); // 出力:[object Array]
let num1 = 123;
let num2 = new Number(123);
console.log(typeof num1); // 出力:number
console.log(typeof num2); // 出力:object
console.log(toString.call(num1)); // 出力:[object Number]
console.log(toString.call(num2)); // 出力:[object Number]
let obj1 = { x:1, y:2, z:3 };
let obj2 = new Object( { x:1, y:2, z:3 } );
console.log(typeof obj1); // 出力:object
console.log(typeof obj2); // 出力:object
console.log(toString.call(obj1)); // 出力:[object Object]
console.log(toString.call(obj2)); // 出力:[object Object]
let str1 = "string";
let str2 = new String("string");
console.log(typeof str1); // 出力:string
console.log(typeof str2); // 出力:object
console.log(toString.call(str1)); // 出力:[object String]
console.log(toString.call(str2)); // 出力:[object String]
let myObj = new Object();
console.log(toString.call(myObj)); // 出力:[object Object]
myObj[Symbol.toStringTag] = "MyObject";
console.log(toString.call(myObj)); // 出力:[object MyObject]