This translation is incomplete. この記事の翻訳にご協力ください
関数のソースコードを表す文字列を返します。
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
構文
function.toString()
戻り値
A string representing the source code of the function.
説明
Function オブジェクトは、Object から継承する toString メソッドをオーバーライドします。つまり、Object.prototype.toString を継承しません。Function オブジェクトについて、toString メソッドは関数宣言を表現するオブジェクトを表す文字列を返します。よって、toString は関数を逆コンパイルして function キーワード、引数の一覧、中かっこ、関数本体のソースコードを含む文字列を返します。
Function が文字列値として表現されるとき、JavaScript は自動的に toString メソッドを呼び出します。例えば、関数が文字列と連結されるときです。
this 値のオブジェクトが Function オブジェクトでない場合、 toString() メソッドは TypeError 例外("Function.prototype.toString called on incompatible object")を発生します。Proxy オブジェクトでも発生します。例えば、
Function.prototype.toString.call('foo'); // TypeError
var proxy = new Proxy(function() {}, {});
Function.prototype.toString.call(proxy); // TypeError
ビルトイン関数オブジェクトや、Function.prototype.bind で関数を作成した場合に toString() を呼ぶと、 toString() は、次のようなネイティブ関数文字列を返します。
"function () {\n [native code]\n}"
If the toString() method is called on a function created by the Function constructor, toString() returns the source code of a synthesized function declaration named "anonymous" using the provided parameters and function body.
例
| Function | Function.prototype.toString の結果 |
|---|---|
function f(){}
|
"function f(){}"
|
class A { a(){} }
|
"class A { a(){} }"
|
function* g(){}
|
"function* g(){}"
|
a => a |
"a => a" |
({ a(){} }.a)
|
"a(){}"
|
({ *a(){} }.a)
|
"*a(){}"
|
({ [0](){} }[0])
|
"[0](){}"
|
Object.getOwnPropertyDescriptor({
get a(){}
}, "a").get
|
"get a(){}"
|
Object.getOwnPropertyDescriptor({
set a(x){}
}, "a").set
|
"set a(x){}"
|
Function.prototype.toString |
"function toString() { [native code] }"
|
(function f(){}.bind(0))
|
"function () { [native code] }"
|
Function("a", "b")
|
"function anonymous(a\n) {\nb\n}"
|
仕様
| 仕様書 | 策定状況 | コメント |
|---|---|---|
| ECMAScript 1st Edition (ECMA-262) | 標準 | 初期定義です。JavaScript 1.1 で実装されました。 |
| ECMAScript 2015 (6th Edition, ECMA-262) Function.prototype.toString の定義 |
標準 | Added more specific requirements for the string representation. |
| Function.prototype.toString revision | Draft | Standardizes native function string, line endings. |
| ECMAScript (ECMA-262) Function.prototype.toString の定義 |
現行の標準 |
ブラウザー実装状況
| デスクトップ | モバイル | サーバー | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
toString | Chrome 完全対応 1 | Edge 完全対応 12 | Firefox 完全対応 1 | IE 完全対応 5 | Opera 完全対応 3 | Safari 完全対応 1 | WebView Android 完全対応 1 | Chrome Android 完全対応 18 | Firefox Android 完全対応 4 | Opera Android 完全対応 10.1 | Safari iOS 完全対応 1 | Samsung Internet Android 完全対応 1.0 | nodejs 完全対応 あり |
| Support of toString revision | Chrome 未対応 なし | Edge 未対応 なし | Firefox 完全対応 54 | IE 未対応 なし | Opera 未対応 なし | Safari 未対応 なし | WebView Android 未対応 なし | Chrome Android 未対応 なし | Firefox Android 完全対応 54 | Opera Android 未対応 なし | Safari iOS 未対応 なし | Samsung Internet Android 未対応 なし | nodejs 未対応 なし |
凡例
- 完全対応
- 完全対応
- 未対応
- 未対応
Firefox 特有のメモ
- Since Firefox 17,
Function.prototype.toString()has been implemented by saving the function's source. The decompiler was removed, so that theindentationparameter is not needed any more. See バグ 761723 for more details. - Firefox 38 以降では、
ProxyオブジェクトでFunction.prototype.toString()を呼ぶと例外を throw します(バグ 1100936)