JavaScript

Object.prototype.toString()メソッド

 編集:2022.07.28 by T.A.

オブジェクトを表す文字列を返します。

obj.toString()
obj Object
戻り値

オブジェクトを表す文字列です。

解説

すべてのオブジェクトは toString メソッドを持ち、オブジェクトが文字列値として表される場面や、文字列が期待される構文で参照されたときに自動的に呼び出されます。既定で、toString() メソッドは Object の子孫にあたるあらゆるオブジェクトに継承されています。このメソッドがカスタム オブジェクト中で上書きされていない場合、toString() は "[object type]" という文字列を返します(type は そのオブジェクトの型)。以下のコードがこれを例示しています。

const o = new Object();
D=o.toString();	//="[object Object]"

注: JavaScript 1.8.5 から、toString() を null に対して呼び出した場合には [object Null] を、undefined に対して呼び出した場合には [object Undefined] を返すようになり、これは ECMAScript 5th Edition とその後のエラッタによって定義されました。
toString() を使用したオブジェクト クラスの検出を参照してください。

引数

Number と BigInt の toString() は、オプションの引数 radix で基数の値を取り、最小値は2で最大値は36です。

radix を使用すると、十進数(1,2,3,4,5,.........)を他の基数の数字に変換することができます。以下の例では十進数を二進数(バイナリ)に変換しています。

let baseTenInt = 10;
D=baseTenInt.toString(2);	//="1010"

BigInt についても同じです。

let bigNum = BigInt(20);
D=bigNum.toString(2);	//="10100"

よくある基数としては次のものがあります:

既定の toString メソッドの上書き

既定の toString() メソッドに代わって呼び出される関数を作ることができます。 toString() メソッドは引数を取らず、文字列を返す必要があります。 toString() メソッドを作成した場合は好きな文字列を返すことができますが、オブジェクトに関する情報を伝えるのが一番役に立つでしょう。

以下のコードは Dog オブジェクト型を定義し、 Dog 型のオブジェクト theDog を生成しています。

function Dog(name, breed, color, sex) {
  this.name = name;
  this.breed = breed;
  this.color = color;
  this.sex = sex;
}

theDog = new Dog('Gabby', 'Lab', 'chocolate', 'female');
//このカスタムオブジェクト上で toString() メソッドを呼び出した場合、メソッドは Object から継承された既定値を返します。
D=theDog.toString();	//="[object Object]"

以下のコードでは、dogToString() を生成および割り当てし、既定の toString() メソッドを上書きします。この関数はオブジェクトの name、breed、color、sex を "property = value;" の形で含む文字列を生成します。

Dog.prototype.toString = function dogToString() {
  const ret = 'Dog ' + this.name + ' is a ' + this.sex + ' ' + this.color + ' ' + this.breed;
  return ret;
}

または、ES6 の テンプレート文字列を使用した例です。

Dog.prototype.toString = function dogToString() {
  return `Dog ${this.name} is a ${this.sex} ${this.color} ${this.breed}`;
}

前者のコードの中で、theDog が文字列の文脈で使用されるたびに、JavaScript は自動的に dogToString() 関数を呼び出し、以下の文字列を返します。

"Dog Gabby is a female chocolate Lab"

toString() を使用したオブジェクトクラスの判別

toString() はすべてのオブジェクトに対し、(既定では)そのクラスを得るために使用することができます。

すべてのオブジェクトで Object.prototype.toString() を使うためには、Function.prototype.call() または Function.prototype.apply() を、第1引数(thisArg)に調べたいオブジェクトを渡して呼び出す必要があります。

const toString = Object.prototype.toString;

D=toString.call(new Date);	//="[object Date]"
D=toString.call(new String);	//="[object String]"
D=toString.call(Math);	//="[object Math]"

// JavaScript 1.8.5 以降
D=toString.call(undefined);	//="[object Undefined]"
D=toString.call(null);	//="[object Null]"

このような toString() の使用は信頼できません。

オブジェクトは Object.prototype.toString() の動作を Symbol.toStringTag プロパティを定義することで変更でき、それによって次のように予想外の動作になります。

const myDate = new Date();
D=Object.prototype.toString.call(myDate);	//="[object Date]"

myDate[Symbol.toStringTag] = 'myDate';
D=Object.prototype.toString.call(myDate);	//="[object myDate]"

Date.prototype[Symbol.toStringTag] = 'prototype polluted';
D=Object.prototype.toString.call(new Date());	//="[object prototype polluted]"

テスト