As the ECMAScript 5.1 specification says, the toString
method returns a string that has the syntax of FunctionDeclaration:
Function.prototype.toString ( )
An implementation-dependent representation of the function is returned. This representation has the syntax of a FunctionDeclaration. Note in particular that the use and placement of white space, line terminators, and semicolons within the representation String is implementation-dependent.
The toString
function is not generic; it throws a TypeError exception if its this value is not a Function object. Therefore, it cannot be transferred to other kinds of objects for use as a method.
FunctionDeclaration has the following syntax:
FunctionDeclaration :
function Identifier ( FormalParameterListopt ) { FunctionBody }
FormalParameterList :
Identifier
FormalParameterList , Identifier
And Identifier defined like:
Identifier ::
IdentifierName but not ReservedWord
IdentifierName ::
IdentifierStart
IdentifierName IdentifierPart
IdentifierStart ::
UnicodeLetter
$
_
\ UnicodeEscapeSequence
IdentifierPart ::
IdentifierStart
UnicodeCombiningMark
UnicodeDigit
UnicodeConnectorPunctuation
Conclusion
Although it isn't a beautiful way to get the function name (but the only way in ES5), if you make it parse all of the possibilities listed above, it can work safely in ES5.
But the ES6 standard modified the specifications for .toString()
, which implies more possible syntaxes, making it unsafe to use this method in it.
So, use this method only in versions prior to ES6.