toString() method returns a string representing the source code of the function.

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 overrides the toString method inherited from Object ; it does not inherit Object.prototype.toString . For user-defined Function objects, the toString method returns a string containing the source text segment which was used to define the function.

JavaScript calls the toString method automatically when a Function is to be represented as a text value, e.g. when a function is concatenated with a string.

toString() method will throw a TypeError exception ("Function.prototype.toString called on incompatible object"), if its this value object is not a Function 对象。

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

toString() method is called on built-in function objects or a function created by Function.prototype.bind , toString() 返回 native function string which looks like

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

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.

范例

Comparing actual source code and toString results

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 (ECMA-262)
The definition of 'Function.prototype.toString' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
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 Yes
Support of toString revision Chrome No Edge No Firefox 54 IE No Opera No Safari No WebView Android No Chrome Android No Firefox Android 54 Opera Android No Safari iOS No Samsung Internet Android No nodejs No

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: