連想配列・Object【オブジェクト】オブジェクト

メモ (外部リンク) コンストラクタ プロパティ 一覧 メソッド 一覧 オブジェクトリテラル プロパティの変更・追加・削除 防止

メモ

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)

コンストラクタ

プロパティ 一覧

プロパティ説明
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 ) 代入監視開始

オブジェクトリテラル

{ [ prop1 [, prop2 [, … propN] ] [,] ] }
  • prop1 ~ propN:プロパティ定義
    詳細例:オブジェクトリテラル・new Object【コンストラクタ】の例
    プロパティ定義説明
    CoverInitializedName var xyz = 'xyz';
    var obj = { xyz };
    CoverInitializedName:変数名
    プロパティ名:変数名
    プロパティ値:変数値
    PropName : Expression ・xyz : "xyz"
    ・func : function (~) {}
    PropName:プロパティ名 (文字列に変換)
    Expression:プロパティ値 (式)
    NumericLiteral:数値 (文字列に変換)

    [PropName]:文字列で記述・式も可能
    "PropName" : Expression"xyz" : "xyz"
    NumericLiteral : Expression123 : "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:処理

プロパティの変更・追加・削除 防止


例 (プロパティ)

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]