blob: bd433a5b99a869712777b85956440edabb9439c8 (
plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
---
title: Function.prototype.toString()
slug: Web/JavaScript/Reference/Global_Objects/Function/toString
tags:
- Function
- JavaScript
- Method
- Prototype
translation_of: Web/JavaScript/Reference/Global_Objects/Function/toString
---
<div>{{JSRef}}</div>
<p><code><strong>toString()</strong></code> メソッドは、関数のソースコードを表す文字列を返します。</p>
<div>{{EmbedInteractiveExample("pages/js/function-tostring.html")}}</div>
<div class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</div>
<h2 id="Syntax" name="Syntax">構文</h2>
<pre class="syntaxbox notranslate"><var>function</var>.toString()</pre>
<h3 id="Return_value" name="Return_value">返値</h3>
<p>関数のソースコードを表す文字列です。</p>
<h2 id="Description" name="Description">解説</h2>
<p>{{jsxref("Function")}} オブジェクトは、 {{jsxref("Object")}} から継承した {{jsxref("Object.prototype.toString", "toString")}} メソッドをオーバーライドします。つまり、 {{jsxref("Object.prototype.toString")}} を継承しません。 {{jsxref("Function")}} オブジェクトについて、 <code>toString</code> メソッドは関数宣言を表現するオブジェクトを表す文字列を返します。</p>
<p>{{jsxref("Function")}} を文字列値として表現するとき、JavaScript は自動的に <code>toString</code> メソッドを呼び出します。例えば、関数が文字列と連結されるときです。</p>
<p><code>this</code> 値のオブジェクトが <code>Function</code> オブジェクトでない場合、 <code>toString()</code> メソッドは {{jsxref("TypeError")}} 例外 ("Function.prototype.toString called on incompatible object") を発生します。</p>
<pre class="brush: js example-bad notranslate">Function.prototype.toString.call('foo'); // TypeError
</pre>
<p><code>toString()</code> メソッドが組込み関数オブジェクトや <code>Function.prototype.bind</code> 作成された関数に対して呼び出されると、 <code>toString()</code> は、次のような<em>ネイティブ関数文字列</em>を返します。</p>
<pre class="brush: js notranslate">"function () {\n [native code]\n}"
</pre>
<p><code>toString()</code> メソッドが <code>Function</code> コンストラクターで生成された関数に対して呼び出されると、 <code>toString()</code> は "anonymous" という名前の関数宣言に、提供された引数と関数の本体を合成したソースコードを返します。</p>
<p><code>+</code> 演算子を使用して、関数の文字列表現を明示的に取得することもできます。</p>
<pre class="brush: js notranslate">function foo() { return 'bar' }
console.log(foo + ''); // "function foo() { return 'bar' }"</pre>
<h2 id="Examples" name="Examples">例</h2>
<h3 id="Comparing_actual_source_code_and_toString_results" name="Comparing_actual_source_code_and_toString_results">実際のソースコードと toString の結果との比較</h3>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Function</th>
<th scope="col">Function.prototype.toString の結果</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<pre class="notranslate">
function f(){}</pre>
</td>
<td>
<pre class="notranslate">
"function f(){}"</pre>
</td>
</tr>
<tr>
<td>
<pre class="notranslate">
class A { a(){} }</pre>
</td>
<td>
<pre class="notranslate">
"class A { a(){} }"</pre>
</td>
</tr>
<tr>
<td>
<pre class="notranslate">
function* g(){}</pre>
</td>
<td>
<pre class="notranslate">
"function* g(){}"</pre>
</td>
</tr>
<tr>
<td>
<pre class="notranslate">
a => a</pre>
</td>
<td>
<pre class="notranslate">
"a => a"</pre>
</td>
</tr>
<tr>
<td>
<pre class="notranslate">
({ a(){} }.a)</pre>
</td>
<td>
<pre class="notranslate">
"a(){}"</pre>
</td>
</tr>
<tr>
<td>
<pre class="notranslate">
({ *a(){} }.a)</pre>
</td>
<td>
<pre class="notranslate">
"*a(){}"</pre>
</td>
</tr>
<tr>
<td>
<pre class="notranslate">
({ [0](){} }[0])</pre>
</td>
<td>
<pre class="notranslate">
"[0](){}"</pre>
</td>
</tr>
<tr>
<td>
<pre class="notranslate">
Object.getOwnPropertyDescriptor({
get a(){}
}, "a").get</pre>
</td>
<td>
<pre class="notranslate">
"get a(){}"</pre>
</td>
</tr>
<tr>
<td>
<pre class="notranslate">
Object.getOwnPropertyDescriptor({
set a(x){}
}, "a").set</pre>
</td>
<td>
<pre class="notranslate">
"set a(x){}"</pre>
</td>
</tr>
<tr>
<td>
<pre class="notranslate">
Function.prototype.toString</pre>
</td>
<td>
<pre class="notranslate">
"function toString() { [native code] }"</pre>
</td>
</tr>
<tr>
<td>
<pre class="notranslate">
(function f(){}.bind(0))</pre>
</td>
<td>
<pre class="notranslate">
"function () { [native code] }"</pre>
</td>
</tr>
<tr>
<td>
<pre class="notranslate">
Function("a", "b")</pre>
</td>
<td>
<pre class="notranslate">
"function anonymous(a\n) {\nb\n}"</pre>
</td>
</tr>
</tbody>
</table>
<h2 id="Specifications" name="Specifications">仕様書</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">仕様書</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('ESDraft', '#sec-function.prototype.tostring', 'Function.prototype.toString')}}</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>
<div>
<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div>
<p>{{Compat("javascript.builtins.Function.toString")}}</p>
</div>
<h2 id="See_also" name="See_also">関連情報</h2>
<ul>
<li>{{jsxref("Object.prototype.toString()")}}</li>
</ul>
|