toString of anonymous function

James Graham jgraham at
Fri Aug 28 06:41:16 PDT 2009

The text in Function.prototype.toString () says:

"An implementation-dependent representation of the function is returned. 
This representation has the syntax of a FunctionDeclaration"

Is there a reason this is a FunctionDeclaration rather than a 
FunctionExpression? In particular what is the expected behavior for 
toString of an anonymous function?

var f = function() {}

In the engines I tried this gave a result like

"function () {}"

with only whitespace differences. This doesn't match the 
FunctionDeclaration production due to the lack of an identifier. Whilst 
the engine could invent an identifier, I don't see what the advantage 
would be and I would be worried about breaking existing code by doing 
so. Instead, I think it would be nice to align the spec with reality here.

More information about the es-discuss mailing list