Function.prototype.toString Change Proposal

Garrett Smith dhtmlkitchen at
Fri Sep 18 00:10:07 PDT 2009

On Thu, Sep 17, 2009 at 11:57 PM, Oliver Hunt <oliver at> wrote:
> On Sep 17, 2009, at 11:52 PM, Garrett Smith wrote:
>> ES5 Committee,
>> In Function.prototype.toString, the representation should be a
>> "Function Definition". The current text requires a
>> "FunctionDeclaration". Many (if not most) implementations today, do
>> not follow the standard in the following cases:
>> 1) anonymous function
>> 2) native code
>> Example:-
>> javascript: alert([parseInt, function(){}].join('\n\n'));
>> Many browsers produce a result something like:
>> function parseInt() {
>>   [native code]
>> }
>> function () {
>> }
>> An implementation could probably change the first to produce a result such
>> as:-
>> function () { /* [native code] */ }
>> That should be harmless, and wacky scripts that try to parse [native
>> code] could continue to do so without breaking.
>> For an anonymous function, an implementation could produce:-
>> function anonymous(){}
>> But that would be a bit odd, as the function does not have the
>> identifier "anonymous".
>> The specification should allow FunctionExpression, so that:-
>> function(){}
> Alas these changes would break things -- In JSC we have had many bug reports
> due about site breakage, which ended up being due to slight changes in in
> the formatting of function [[ToString]] conversion.  Any changes to it are
> immensely risky (we've had at least a couple of bugs that were due to
> specific spaces and newlines not being in the "right" place), the degree of
> changes you're asking for are almost certain to break sites.


javascript: alert(function(){})

Safari 4 elerts:
"function () {}"

The current specification doesn't allow that.

Why not?

The current specification requires the result of
Function.prototype.toString to be a FunctionDeclaration and
"function(){}" is not  a FunctionDeclaration representation.

So it seems you want Safari to change, but the specification to
remain. I would have to disagree, as that might break sites (and for
absolutely no good reason).

I propose that browsers today stay as they are (Safari, Firefox,
Chrome, IE, etc all produce that result).  The specification should
allow them to do so.


More information about the es-discuss mailing list