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/function/tostring/index.html | 216 +++++++++++++++++++++ 1 file changed, 216 insertions(+) create mode 100644 files/ja/web/javascript/reference/global_objects/function/tostring/index.html (limited to 'files/ja/web/javascript/reference/global_objects/function/tostring') diff --git a/files/ja/web/javascript/reference/global_objects/function/tostring/index.html b/files/ja/web/javascript/reference/global_objects/function/tostring/index.html new file mode 100644 index 0000000000..bd433a5b99 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/function/tostring/index.html @@ -0,0 +1,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 +--- +
{{JSRef}}
+ +

toString() メソッドは、関数のソースコードを表す文字列を返します。

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

構文

+ +
function.toString()
+ +

返値

+ +

関数のソースコードを表す文字列です。

+ +

解説

+ +

{{jsxref("Function")}} オブジェクトは、 {{jsxref("Object")}} から継承した {{jsxref("Object.prototype.toString", "toString")}} メソッドをオーバーライドします。つまり、 {{jsxref("Object.prototype.toString")}} を継承しません。 {{jsxref("Function")}} オブジェクトについて、 toString メソッドは関数宣言を表現するオブジェクトを表す文字列を返します。

+ +

{{jsxref("Function")}} を文字列値として表現するとき、JavaScript は自動的に toString メソッドを呼び出します。例えば、関数が文字列と連結されるときです。

+ +

this 値のオブジェクトが Function オブジェクトでない場合、 toString() メソッドは {{jsxref("TypeError")}} 例外 ("Function.prototype.toString called on incompatible object") を発生します。

+ +
Function.prototype.toString.call('foo'); // TypeError
+
+ +

toString() メソッドが組込み関数オブジェクトや Function.prototype.bind 作成された関数に対して呼び出されると、 toString() は、次のようなネイティブ関数文字列を返します。

+ +
"function () {\n    [native code]\n}"
+
+ +

toString() メソッドが Function コンストラクターで生成された関数に対して呼び出されると、 toString() は "anonymous" という名前の関数宣言に、提供された引数と関数の本体を合成したソースコードを返します。

+ +

+ 演算子を使用して、関数の文字列表現を明示的に取得することもできます。

+ +
function foo() { return 'bar' }
+console.log(foo + ''); // "function foo() { return 'bar' }"
+ +

+ +

実際のソースコードと toString の結果との比較

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FunctionFunction.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}"
+
+ +

仕様書

+ + + + + + + + + + + + +
仕様書
{{SpecName('ESDraft', '#sec-function.prototype.tostring', 'Function.prototype.toString')}}
+ +

ブラウザーの互換性

+ +
+ + +

{{Compat("javascript.builtins.Function.toString")}}

+
+ +

関連情報

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