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.
| 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. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
完整支持
不支持
Function
Function.prototype.apply()
Function.prototype.bind()
Function.prototype.call()
Function.prototype.toSource()
Function.prototype.toString()
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()