本文目录

JS function toString()

🌙
手机阅读
本文目录结构

toString() 方法返回一个表示当前函数源代码的字符串。

语法

function.toString()

返回值

表示函数源代码的一个字符串

描述

Function对象覆盖了从Object继承来的toString 方法。对于用户定义的 Function 对象,toString方法返回一个字符串,其中包含用于定义函数的源文本段。

Function需要转换为字符串时,通常会自动调用函数的 toString 方法。

this 不是 Function 对象,则 toString() 方法将抛出 TypeError  (“Function.prototype.toString called on incompatible object”) 异常,比如 Proxy 对象就会抛出异常。

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

如果是在内置函数或由 Function.prototype.bind 返回的函数上调用 toString(),则toString() 返回原生代码字符串,如下

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

若是在由 Function 构造器生成的函数上调用 toString() ,则 toString() 返回创建后的函数源码,包括形参和函数体,函数名为 “anonymous”。

示例

Function Function.prototype.toString result
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)](https://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%201st%20edition,%20June%201997.pdf "ECMAScript 1st Edition ECMA-262") | Standard | 初始定义。在 JavaScript 1.1 中实现。 |
| [ECMAScript 2015 (6th Edition, ECMA-262)Function.prototype.toString](https://www.ecma-international.org/ecma-262/6.0/#sec-function.prototype.tostring) | Standard | 对字符串表示增加了更多的特定需求。 |
| [Function.prototype.toString revision](http://tc39.github.io/Function-prototype-toString-revision/) | Draft | 对内置函数与行尾表示进行标准化。 |
| [ECMAScript Latest Draft (ECMA-262)Function.prototype.toString](https://tc39.es/ecma262/#sec-function.prototype.tostring) | Draft |   |

## 附注(针对Firefox)

-   从Firefox 17开始,`Function.prototype.toString()` 通过保存函数源码的方式来实现,而之前是通过反编译器反编译函数字节码的方式来实现。反编译器已经被移除,因此我们不再需要 `indentation` 参数。查看 [bug 761723](https://bugzilla.mozilla.org/show_bug.cgi?id=761723 "FIXED: implement toString of function objects by saving source") 获得更多信息。
-   从Firefox 38开始,`Function.prototype.toString()` 会对 [`Proxy`](/api/js-es/ob-proxy/overview.html "Proxy 对象用于定义基本操作的自定义行为(如属性查找,赋值,枚举,函数调用等)。") 对象抛出异常 ([bug 1100936](https://bugzilla.mozilla.org/show_bug.cgi?id=1100936 "FIXED: Handle various operations on revoked proxies"))。

## 相关链接

-   [`Object.prototype.toString()`](/api/js-es/ob-object/to-string.html "方法返回一个表示该对象的字符串。")

AXIHE / 精选资源

浏览全部教程

面试题

学习网站

前端培训
自己甄别

前端书籍

关于朱安邦

我叫 朱安邦,阿西河的站长,在杭州。

以前是一名平面设计师,后来开始接接触前端开发,主要研究前端技术中的JS方向。

业余时间我喜欢分享和交流自己的技术,欢迎大家关注我的 Bilibili

关注我: Github / 知乎

于2021年离开前端领域,目前重心放在研究区块链上面了

我叫朱安邦,阿西河的站长

目前在杭州从事区块链周边的开发工作,机械专业,以前从事平面设计工作。

2014年底脱产在老家自学6个月的前端技术,自学期间几乎从未出过家门,最终找到了满意的前端工作。更多>

于2021年离开前端领域,目前从事区块链方面工作了