Summary
The toString()
method returns a string representing the source code of the function.
Syntax
function.toString(indentation)
Parameters
- indentation Obsolete since Gecko 17
-
The amount of spaces to indent the string representation of the source code. If
indentation
is less than or equal to-1
, most unnecessary spaces are removed.
Description
The Function
object overrides the toString
method inherited from Object
; it does not inherit Object.prototype.toString
. For Function
objects, the toString
method returns a string representation of the object in the form of a function declaration. That is, toString decompiles the function, and the string returned includes the function
keyword, the argument list, curly braces, and the source of the function body.
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.
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition. Implemented in JavaScript 1.1. | Standard | Initial definition. |
ECMAScript Language Specification 5.1th Edition (ECMA-262) | Standard | |
ECMAScript Language Specification 6th Edition (ECMA-262) | Draft |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Gecko-specific notes
Since Gecko 17.0 (Firefox 17 / Thunderbird 17 / SeaMonkey 2.14), Function.prototype.toString()
has been implemented by saving the function's source. The decompiler was removed, so that the indentation
parameter is not needed any more. See bug 761723 for more details.