aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/function/tostring
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/function/tostring')
-rw-r--r--files/zh-cn/web/javascript/reference/global_objects/function/tostring/index.html230
1 files changed, 230 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/function/tostring/index.html b/files/zh-cn/web/javascript/reference/global_objects/function/tostring/index.html
new file mode 100644
index 0000000000..01bf4d2ce7
--- /dev/null
+++ b/files/zh-cn/web/javascript/reference/global_objects/function/tostring/index.html
@@ -0,0 +1,230 @@
+---
+title: Function.prototype.toString()
+slug: Web/JavaScript/Reference/Global_Objects/Function/toString
+tags:
+ - Function
+ - JavaScript
+ - Method
+translation_of: Web/JavaScript/Reference/Global_Objects/Function/toString
+---
+<div>{{JSRef}}</div>
+
+<p><strong><code>toString()</code> </strong>方法返回一个表示当前函数源代码的字符串。</p>
+
+<div>{{EmbedInteractiveExample("pages/js/function-tostring.html")}}</div>
+
+<h2 id="Syntax" name="Syntax">语法</h2>
+
+<pre class="syntaxbox"><code><var>function</var>.toString()</code></pre>
+
+<h3 id="返回值">返回值</h3>
+
+<p>表示函数源代码的一个字符串</p>
+
+<h2 id="Description" name="Description">描述</h2>
+
+<p>{{jsxref("Function")}}对象覆盖了从{{jsxref("Object")}}继承来的{{jsxref("Object.prototype.toString", "toString")}} 方法。对于用户定义的 {{jsxref("Function")}} 对象,<code>toString</code>方法返回一个字符串,其中包含用于定义函数的源文本段。</p>
+
+<p>在{{jsxref("Function")}}需要转换为字符串时,通常会自动调用函数的 <code>toString </code>方法。</p>
+
+<p>若 <code>this</code> 不是 <code>Function </code>对象,则 <code>toString()</code> 方法将抛出 {{jsxref("TypeError")}}  ("Function.prototype.toString called on incompatible object") 异常,比如 {{jsxref("Proxy")}} 对象就会抛出异常。</p>
+
+<pre class="brush: js example-bad">Function.prototype.toString.call('foo'); // TypeError
+</pre>
+
+<p>如果是在内置函数或由 <code>Function.prototype.bind </code>返回的函数上调用 <code>toString()</code>,则<code>toString()</code> 返回原生代码字符串,如下</p>
+
+<pre class="brush: js">"function () {\n    [native code]\n}"
+</pre>
+
+<p>若是在由 <code>Function</code> 构造器生成的函数上调用 <code>toString()</code> ,则 <code>toString()</code> 返回创建后的函数源码,包括形参和函数体,函数名为 "anonymous"。</p>
+
+<h2 id="示例">示例</h2>
+
+<table class="standard-table">
+ <thead>
+ <tr>
+ <th scope="col">Function</th>
+ <th scope="col">Function.prototype.toString result</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>
+ <pre>
+function f(){}</pre>
+ </td>
+ <td>
+ <pre>
+"function f(){}"</pre>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ <pre>
+class A { a(){} }</pre>
+ </td>
+ <td>
+ <pre>
+"class A { a(){} }"</pre>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ <pre>
+function* g(){}</pre>
+ </td>
+ <td>
+ <pre>
+"function* g(){}"</pre>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ <pre>
+a =&gt; a</pre>
+ </td>
+ <td>
+ <pre>
+"a =&gt; a"</pre>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ <pre>
+({ a(){} }.a)</pre>
+ </td>
+ <td>
+ <pre>
+"a(){}"</pre>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ <pre>
+({ *a(){} }.a)</pre>
+ </td>
+ <td>
+ <pre>
+"*a(){}"</pre>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ <pre>
+({ [0](){} }[0])</pre>
+ </td>
+ <td>
+ <pre>
+"[0](){}"</pre>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ <pre>
+Object.getOwnPropertyDescriptor({
+  get a(){}
+}, "a").get</pre>
+ </td>
+ <td>
+ <pre>
+"get a(){}"</pre>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ <pre>
+Object.getOwnPropertyDescriptor({
+  set a(x){}
+}, "a").set</pre>
+ </td>
+ <td>
+ <pre>
+"set a(x){}"</pre>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ <pre>
+Function.prototype.toString</pre>
+ </td>
+ <td>
+ <pre>
+"function toString() { [native code] }"</pre>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ <pre>
+(function f(){}.bind(0))</pre>
+ </td>
+ <td>
+ <pre>
+"function () { [native code] }"</pre>
+ </td>
+ </tr>
+ <tr>
+ <td>
+ <pre>
+Function("a", "b")</pre>
+ </td>
+ <td>
+ <pre>
+"function anonymous(a\n) {\nb\n}"</pre>
+ </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="规范" style="margin-bottom: 20px; line-height: 30px;">规范</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">规范版本</th>
+ <th scope="col">规范状态</th>
+ <th scope="col">注解</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES1')}}</td>
+ <td>{{Spec2('ES1')}}</td>
+ <td>初始定义。在 JavaScript 1.1 中实现。</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES6', '#sec-function.prototype.tostring', 'Function.prototype.toString')}}</td>
+ <td>{{Spec2('ES6')}}</td>
+ <td>对字符串表示增加了更多的特定需求。</td>
+ </tr>
+ <tr>
+ <td><a href="http://tc39.github.io/Function-prototype-toString-revision/">Function.prototype.toString revision</a></td>
+ <td>Draft</td>
+ <td>对内置函数与行尾表示进行标准化。</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-function.prototype.tostring', 'Function.prototype.toString')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="浏览器兼容性">浏览器兼容性</h2>
+
+<div>
+
+
+<p>{{Compat("javascript.builtins.Function.toString")}}</p>
+</div>
+
+<h2 id="附注(针对Firefox)">附注(针对Firefox)</h2>
+
+<ul>
+ <li>从Firefox 17开始,<code>Function.prototype.toString()</code> 通过保存函数源码的方式来实现,而之前是通过反编译器反编译函数字节码的方式来实现。反编译器已经被移除,因此我们不再需要 <code>indentation</code> 参数。查看 {{bug("761723")}} 获得更多信息。</li>
+ <li>从Firefox 38开始,<code>Function.prototype.toString()</code> 会对 {{jsxref("Proxy")}} 对象抛出异常 ({{bug(1100936)}})。</li>
+</ul>
+
+<h2 id="See_also" name="See_also">相关链接</h2>
+
+<ul>
+ <li>{{jsxref("Object.prototype.toString()")}}</li>
+</ul>