Repository navigation
Expand file tree
/
Copy pathindex.md
More file actions
73 lines (46 loc) · 2.56 KB
/
Copy pathindex.md
File metadata and controls
73 lines (46 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
---
title: String.prototype.toString()
short-title: toString()
slug: Web/JavaScript/Reference/Global_Objects/String/toString
l10n:
sourceCommit: 544b843570cb08d1474cfc5ec03ffb9f4edc0166
---
**`toString()`** は {{jsxref("String")}} 値のメソッドで、指定されたオブジェクトを表す文字列を返します。
{{InteractiveExample("JavaScript デモ: String.prototype.toString()")}}
```js interactive-example
const stringObj = new String("foo");
console.log(stringObj);
// 予想される結果: String { "foo" }
console.log(stringObj.toString());
// 予想される結果: "foo"
```
## 構文
```js-nolint
toString()
```
### 引数
なし。
### 返値
呼び出したオブジェクトを表す文字列です。
## 詳細
その {{jsxref("String")}} オブジェクトは、{{jsxref("Object")}} の `toString` メソッドを上書きします。つまり、{{jsxref("Object.prototype.toString()")}} を継承しません。`String` オブジェクトの `toString` メソッドは、そのオブジェクトを表す文字列を返します({{jsxref("String.prototype.valueOf()")}} と同等です)。
`toString()` メソッドは `this` 値が `String` プリミティブまたはラッパーオブジェクトであることを要求します。他の `this` の値に対しては、文字列値に変換しようとせずに {{jsxref("TypeError")}} を発生します。
`String` には [`[Symbol.toPrimitive]()`](/ja/docs/Web/JavaScript/Reference/Global_Objects/Symbol/toPrimitive) メソッドがないため、JavaScript は `toString()` メソッドを、文字列が求められるコンテキスト、例えば[テンプレートリテラル](/ja/docs/Web/JavaScript/Reference/Template_literals)で `String` オブジェクトが使用されると、自動的に呼び出します。しかし、文字列プリミティブ値は[文字列に変換する](/ja/docs/Web/JavaScript/Reference/Global_Objects/String#string_coercion)ために `toString()` を呼び出しません。既に文字列なので、変換が実施されないからです。
```js
String.prototype.toString = () => "Overridden";
console.log(`${"foo"}`); // "foo"
console.log(`${new String("foo")}`); // "Overridden"
```
## 例
### toString() メソッドを使う
以下の例は、{{jsxref("String")}} オブジェクトを表す文字列の値を表示します。
```js
const x = new String("Hello world");
console.log(x.toString()); // "Hello world"
```
## 仕様書
{{Specifications}}
## ブラウザーの互換性
{{Compat}}
## 関連情報
- {{jsxref("String.prototype.valueOf()")}}