--- title: Object slug: Web/JavaScript/Reference/Global_Objects/Object tags: - Constructor - JavaScript - Object - コンストラクター translation_of: Web/JavaScript/Reference/Global_Objects/Object ---
Object
クラスは JavaScript のデータ型の一つを表します。これは様々なキー付きコレクションとより複雑な実態を格納するために使用されます。Object は {{jsxref("Object/Object", "Object()")}} コンストラクターまたはオブジェクト初期化子/リテラル構文を使用して生成することができます。
JavaScript のほぼすべてのオブジェクトが {{jsxref("Object")}} のインスタンスです。一般的なオブジェクトは、プロパティを (メソッドを含めて) Object.prototype
から継承していますが、これらのプロパティはシャドウ化 (別名オーバーライド) されている場合があります。しかし、意図的にそうではない Object
を生成したり (例えば {{jsxref("Object.create", "Object.create(null)")}} によって)、変更した結果そうではなくなる場合 (例えば {{jsxref("Object.setPrototypeOf")}}) もあります。
Object
プロトタイプオブジェクトへの変更は、その変更の対象となるプロパティやメソッドがプロトタイプチェーンに沿ってさらにオーバーライドされない限り、プロトタイプチェーンを通してすべてのオブジェクトに表示されます。これはとても強力ですが、オブジェクトの動作をオーバーライドしたり拡張したりするのは潜在的に危険をはらむ仕組みでもあります。
Object
コンストラクターは、指定された値のオブジェクトラッパーを生成します。
コンストラクター以外のコンテキストで呼び出された場合、Object
は new Object()
と同等に動作します。
オブジェクト初期化子/リテラル構文も参照してください。
オブジェクト自体には、自身のプロパティを削除するメソッドはありません ( {{jsxref("Map.prototype.delete", "Map.prototype.delete()")}}) など)。これを行うには、delete 演算子を使用する必要があります。
Object
コンストラクターは指定された値のオブジェクトラッパーを生成します。[key, value]
ペアを含む配列を返します。[key, value]
の組から新しいオブジェクトを返します。(これは {{jsxref("Object.entries")}} の逆です。)[[Prototype]]
プロパティ) を返します。NaN
値はすべて同じとして扱われます (抽象的等価比較とも厳密等価比較とも異なります)。[[Prototype]]
プロパティ) を設定します。undefined
と null
データ型を与えられた Object
を使用する次の例は、o
に空の Object
オブジェクトを格納します。
let o = new Object()
let o = new Object(undefined)
let o = new Object(null)
Boolean
オブジェクトの生成に Object
を使用する次の例は、o
に {{jsxref("Boolean")}} オブジェクトを格納します。
// o = new Boolean(true) に同じ let o = new Object(true)
// to o = new Boolean(false) に同じ let o = new Object(Boolean())
Object.prototype
の既存のメソッドの動作を変更する場合は、既存のロジックの前または後で独自の拡張を囲む形でコードを挿入するようにしてください。例えば、この (テストされていない) コードは、組込みロジックや誰かの拡張機能が実行される前に、条件付きで独自のロジックを実行します。
関数が呼び出されると、呼び出す引数は配列状「変数」 arguments に保持されます。例えば myFn(a, b, c)
の呼び出しでは、myFn の本体内での引数は (a, b, c) に対応する 3 つの配列状要素を含みます。
フックを使ってプロトタイプを変更する場合は、関数で apply()
を呼び出すことで、this
と引数 (呼び出し状態) を現在の動作に渡します。このパターンは、Node.prototype
や Function.prototype
など、どんなプロトタイプにも使えます。
var current = Object.prototype.valueOf; // Since my property "-prop-value" is cross-cutting and isn't always // on the same prototype chain, I want to modify Object.prototype: Object.prototype.valueOf = function() { if (this.hasOwnProperty('-prop-value')) { return this['-prop-value']; } else { // It doesn't look like one of my objects, so let's fall back on // the default behavior by reproducing the current behavior as best we can. // The apply behaves like "super" in some other languages. // Even though valueOf() doesn't take arguments, some other hook may. return current.apply(this, arguments); } }
JavaScript はサブクラスオブジェクトを持っていないため、プロトタイプはオブジェクトとして機能する特定の関数の「基本クラス」オブジェクトを作成するための有用な回避策です。例えば、以下のようになります。
var Person = function(name) { this.name = name; this.canTalk = true; }; Person.prototype.greet = function() { if (this.canTalk) { console.log('Hi, I am ' + this.name); } }; var Employee = function(name, title) { Person.call(this, name); this.title = title; }; Employee.prototype = Object.create(Person.prototype); Employee.prototype.constructor = Employee; //If you don't set Object.prototype.constructor to Employee, //it will take prototype.constructor of Person (parent). //To avoid that, we set the prototype.constructor to Employee (child). Employee.prototype.greet = function() { if (this.canTalk) { console.log('Hi, I am ' + this.name + ', the ' + this.title); } }; var Customer = function(name) { Person.call(this, name); }; Customer.prototype = Object.create(Person.prototype); Customer.prototype.constructor = Customer; //If you don't set Object.prototype.constructor to Customer, //it will take prototype.constructor of Person (parent). //To avoid that, we set the prototype.constructor to Customer (child). var Mime = function(name) { Person.call(this, name); this.canTalk = false; }; Mime.prototype = Object.create(Person.prototype); Mime.prototype.constructor = Mime; //If you don't set Object.prototype.constructor to Mime, //it will take prototype.constructor of Person (parent). //To avoid that, we set the prototype.constructor to Mime (child). var bob = new Employee('Bob', 'Builder'); var joe = new Customer('Joe'); var rg = new Employee('Red Green', 'Handyman'); var mike = new Customer('Mike'); var mime = new Mime('Mime'); bob.greet(); // Hi, I am Bob, the Builder joe.greet(); // Hi, I am Joe rg.greet(); // Hi, I am Red Green, the Handyman mike.greet(); // Hi, I am Mike mime.greet();
仕様書 |
---|
{{SpecName('ESDraft', '#sec-object-objects', 'Object')}} |
{{Compat("javascript.builtins.Object")}}