--- title: Object.prototype.toString() slug: Web/JavaScript/Reference/Global_Objects/Object/toString tags: - JavaScript - Method - Object - Prototype translation_of: Web/JavaScript/Reference/Global_Objects/Object/toString ---
toString() メソッドは、オブジェクトを表す文字列を返します。
obj.toString()
オブジェクトを表す文字列です。
すべてのオブジェクトは toString メソッドを持ち、オブジェクトが文字列値として表される場面や、文字列が期待される構文で参照されたときに自動的に呼び出されます。既定で、 toString() メソッドは Object の子孫にあたるあらゆるオブジェクトに継承されています。このメソッドがカスタムオブジェクト中で上書きされていない場合、 toString() は "[object type]" という文字列を返します (type は そのオブジェクトの型)。以下のコードがこれを例示しています。
const o = new Object(); o.toString(); // [object Object] を返す
注: JavaScript 1.8.5 から、 toString() を {{jsxref("null")}} に対して呼び出した場合には [object Null] を、 {{jsxref("undefined")}} に対して呼び出した場合には [object Undefined] を返すようになり、これは ECMAScript 5th Edition とその後のエラッタによって定義されました。
toString() を使用したオブジェクトクラスの検出を参照してください。
Number と BigInt の toString() は、オプションの引数 radix で基数の値を取り、最小値は2で最大値は36です。
radix を使用すると、十進数 (1,2,3,4,5,.........) を他の基数の数字に変換することができます。以下の例では十進数を二進数 (バイナリ) に変換しています。
let baseTenInt = 10; console.log(baseTenInt.toString(2)); // 期待される結果は "1010"
BigInt についても同じです。
let bigNum = BigInt(20); console.log(bigNum.toString(2)); // 期待される結果は "10100"
よくある基数としては次のものがあります。
既定の 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() メソッドを呼び出した場合、メソッドは {{jsxref("Object")}} から継承された既定値を返します。
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 の {{jsxref("Template_literals", "テンプレート文字列", "", 1)}}を使用した例です。
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() はすべてのオブジェクトに対し、(既定では) そのクラスを得るために使用することができます。
すべてのオブジェクトで Object.prototype.toString() を使うためには、 {{jsxref("Function.prototype.call()")}} または {{jsxref("Function.prototype.apply()")}} を、第1引数 (thisArg) に調べたいオブジェクトを渡して呼び出す必要があります。
const toString = Object.prototype.toString; toString.call(new Date); // [object Date] toString.call(new String); // [object String] toString.call(Math); // [object Math] // Since JavaScript 1.8.5 toString.call(undefined); // [object Undefined] toString.call(null); // [object Null]
このような toString() の使用は信頼できません。オブジェクトは Object.prototype.toString() の動作を {{jsxref("Symbol.toStringTag")}} プロパティを定義することで変更でき、それによって次のように予想外の動作になります。
const myDate = new Date(); Object.prototype.toString.call(myDate); // [object Date] myDate[Symbol.toStringTag] = 'myDate'; Object.prototype.toString.call(myDate); // [object myDate] Date.prototype[Symbol.toStringTag] = 'prototype polluted'; Object.prototype.toString.call(new Date()); // [object prototype polluted]
| 仕様書 |
|---|
| {{SpecName('ESDraft', '#sec-object.prototype.tostring', 'Object.prototype.toString')}} |
{{Compat("javascript.builtins.Object.toString")}}