From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/object/tostring/index.html | 169 +++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 files/ja/web/javascript/reference/global_objects/object/tostring/index.html (limited to 'files/ja/web/javascript/reference/global_objects/object/tostring') diff --git a/files/ja/web/javascript/reference/global_objects/object/tostring/index.html b/files/ja/web/javascript/reference/global_objects/object/tostring/index.html new file mode 100644 index 0000000000..a152802a58 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/object/tostring/index.html @@ -0,0 +1,169 @@ +--- +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 +--- +
{{JSRef}}
+ +

toString() メソッドは、オブジェクトを表す文字列を返します。

+ +
{{EmbedInteractiveExample("pages/js/object-prototype-tostring.html")}}
+ + + +

構文

+ +
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() メソッドは引数を取らず、文字列を返す必要があります。 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() を使用したオブジェクトクラスの判別

+ +

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")}}

+
+ +

関連情報

+ + -- cgit v1.2.3-54-g00ecf