JavaScript > Objectオブジェクト

Objectオブジェクト 編集

Objectオブジェクトは、StringやNumber等の他のオブジェクトの雛形として用意されています。 JavaScriptでは他のオブジェクトは、基本的にすべてのデータはObject型を継承しています。

Objectの型
console.log(`Objectの型
typeof Object => ${typeof Object}
typeof String => ${typeof String}
typeof Number => ${typeof Number}
typeof Function => ${typeof Function}

Object === Object.prototype.constructor => ${Object === Object.prototype.constructor}
String === String.prototype.constructor => ${String === String.prototype.constructor}
Number === Number.prototype.constructor => ${Number === Number.prototype.constructor}
Function === Function.prototype.constructor => ${Function === Function.prototype.constructor}
`)
実行結果
Objectの型
typeof Object => function
typeof String => function
typeof Number => function
typeof Function => function

Object === Object.prototype.constructor => true
String === String.prototype.constructor => true
Number === Number.prototype.constructor => true
Function === Function.prototype.constructor => true
Objectの型は Function です。Objectに限らず、String, Number そして Function までも型は Function です。
この関数はそのオブジェクトのconstructorであることがわかります。

静的プロパティ 編集

Object.length 編集

コンストラクターとしてのパラメーター数。

1
Number

Object.name 編集

型名。

"Object"
String

Object.prototype 編集

一般的なオブジェクトは、Object.prototypeからプロパティ(メソッドを含む)を継承しますが、これらのプロパティはシャドウイング(別名オーバーライド)されることがあります。 しかし、Object.create(null)のように意図的に継承されていないオブジェクトを作成したり、Object.setPrototypeOfのように継承されていないオブジェクトを変更することもできます。

静的メソッド 編集

この節は書きかけです。この節を編集してくれる方を心からお待ちしています。

Object.assign() 編集

Object.create() 編集

Object.defineProperties() 編集

Object.defineProperties()は、JavaScriptでオブジェクトのプロパティを定義または変更するためのメソッドです。このメソッドは、単一のオブジェクトに対して複数のプロパティを定義したり、既存のプロパティを変更したりするのに使用されます。

Object.defineProperties()は以下のように使います:

Object.defineProperties(object, descriptors);
  • object: プロパティを定義または変更する対象のオブジェクト。
  • descriptors: プロパティディスクリプタの集合。各プロパティには、そのプロパティの属性(configurable、enumerable、value、writable、get、setなど)が含まれます。

例えば、先程のArrayのプロトタイプに複数の新しいプロパティを追加する例を示します:

Object.defineProperties(Array.prototype, {
  first: {
    get: function() { return this[0]; },
    set: function(e) { return this[0] = e; },
  },
  last: {
    get: function() { return this[this.length - 1]; },
    set: function(e) { return this[this.length - 1] = e; },
  },
  min: {
    get: function() { return Math.min(...this); },
  },
  max: {
    get: function() { return Math.max(...this); },
  },
});

let ary = [3, 1, 4, 1, 5, 9, 2, 6];
console.log(ary.first); // => 3
ary.first = 5;
console.log(ary.first); // => 5
console.log(ary.last);  // => 6
ary.last = 5.1;
console.log(ary.last);  // => 5.1
console.log(ary);  // => Array [5, 1, 4, 1, 5, 9, 2, 5.1]
console.log(ary.min);   // => 1
console.log(ary.max);   // => 9

このようにして、Array.prototypeに新しいプロパティを定義しました。firstlastminmaxプロパティは、それぞれgetsetメソッドを使用して振る舞いを定義しています。

Object.defineProperty() 編集

Object.entries() 編集

Object.freeze() 編集

Object.fromEntries() 編集

Object.getOwnPropertyDescriptor() 編集

Object.getOwnPropertyDescriptors() 編集

Object.getOwnPropertyNames() 編集

Object.getOwnPropertyNames(obj)メソッドは、与えられたオブジェクトobjで直接見つかったすべてのプロパティ(Symbolを使用するものを除く、非列挙型プロパティを含む)の配列を返します[1]。 Object.getOwnPropertyNames()は、与えられたオブジェクト obj に直接見られる列挙可能なプロパティと列挙不可能なプロパティに対応する文字列を要素とする配列を返します。配列内の列挙可能なプロパティの順序は、オブジェクトのプロパティに対する for...in ループ(または Object.keys())によって示される順序と一致します。ES6 によると、オブジェクトの整数キー(列挙可能なものとそうでないものの両方)は、昇順で最初に配列に追加され、その後に文字列キーが挿入順に続きます。

Object.getOwnPropertySymbols() 編集

Object.getPrototypeOf() 編集

Object.is() 編集

Object.isExtensible() 編集

Object.isFrozen() 編集

Object.isSealed() 編集

Object.keys() 編集

Object.preventExtensions() 編集

Object.seal() 編集

Object.setPrototypeOf() 編集

Object.values() 編集

インスタンスメソッド 編集

この節は書きかけです。この節を編集してくれる方を心からお待ちしています。

Object.prototype.__defineGetter__() 編集

Object.prototype.__defineSetter__() 編集

Object.prototype.__lookupGetter__() 編集

Object.prototype.__lookupSetter__() 編集

Object.prototype.constructor() 編集

Object.prototype.hasOwnProperty() 編集

Object.prototype.isPrototypeOf() 編集

Object.prototype.propertyIsEnumerable() 編集

Object.prototype.toString() 編集

toString()メソッドは、オブジェクトを表す文字列を返します[2]。 すべてのオブジェクトは toString() メソッドを持っています。このメソッドは、オブジェクトがテキスト値として表現される場合や、文字列が期待される方法でオブジェクトが参照される場合に、自動的に呼び出されます。デフォルトでは、toString()メソッドは、Objectの子孫であるすべてのオブジェクトに継承されます。カスタム・オブジェクトでこのメソッドがオーバーライドされていない場合、toString() は "[object type]" を返し、type はオブジェクト・タイプです。以下のコードで説明します。

Object.prototype.valueOf() 編集

Object.prototype.toLocaleString() 編集

基本的にtoStringと全く同じ動作を行う。 このメソッドは、ArrayやNumber、Date型のオブジェクトがtoLocaleStringを持っているのに対し、他のオブジェクトはこれを持っていないため、他の型でtoLocaleStringが呼び出される場合でもエラー出してプログラムの実行が止まらないための雛形として用意されています。 他のデータ型でtoLocaleStringを使うべき問題にあたった場合にそれぞれのデータ型でメソッドを定義してやればよい。

Object.prototype.hasOwnProperty(V) 編集

オブジェクトが引数で渡したプロパティを自前で持っているかをチェックする。持っていない場合、および他のクラスから継承している場合はfalseを返す。

for~in文は通常、オブジェクトにプロパティやメソッドを追加したときにその違いが出る。 例えば次の様なオブジェクトを作成したとする。

Object.prototype.getBMI = function(){ return this[体重] / this[身長] ** 2 }
const girl ={
  身長: 158,
  体重: 49
};
for (const p in girl)
  console.log(p, ' : ', girl[p]); 
/*
 身長  :  158
 体重  :  49
 getBMI  :  ƒ (){ return this[体重] / this[身長] ** 2 }
 */

この様にすると結果として、「身長」、「体重」の他に「getBMI」のメソッドの中身も表示されてしまう。 それを禁止するために、親オブジェクトから取得したデータを無視するため次の様に記述する。

for (const p in girl)
  if (girl.hasOwnProperty(p))
    console.log(p, ' : ', girl[p]);

ちなみに、親オブジェクトも含めてそのプロパティを持っているかどうかを調べるにはin演算子を使用する。

if (p in girl) { /* ... */ }

Object.prototype.valueOf() 編集

データの値をそのまま返す。 このメソッドは他のオブジェクトでそれぞれオーバーライドされるのを期待されて存在しています。 そのため、単体としてそれほどの意味は無い。 プリミティブ型のラッパーオブジェクトから元のプリミティブを得る時に使われる。

Object.prototype.isPrototypeOf(V) 編集

引数で与えたオブジェクトが、自分の子孫ノードであるかどうかをチェックする。

function Foo(){}
function Bar(){}
function Baz(){}
Bar.prototype = new Foo();
Baz.prototype = new Bar();
console.log(Foo.prototype.isPrototypeOf(new Bar()));  // true
console.log(Foo.prototype.isPrototypeOf(new Baz()));  // true

Object.prototype.propertyIsEnumerable(V) 編集

引数で指定したプロパティがオブジェクト自身のプロパティかつ、for~in文で列挙可能なものであるのかをチェックする。 プロパティでなければfalseを、列挙可能ならtrueを、不可能ならfalseを返す。

脚註 編集

  1. ^ Object.getOwnPropertyNames() - JavaScript // MDN” (2021年11月21日). 2021年11月23日閲覧。
  2. ^ Object.prototype.toString() - JavaScript // MDN” (2021年9月13日). 2021年11月23日閲覧。