Function.prototype.toString Change Proposal

Garrett Smith dhtmlkitchen at gmail.com
Thu Sep 17 23:52:11 PDT 2009


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(){}

- would be legal.

This change would have no negative impact on existing scripts, and
would allow existing implementations to continue to exhibit the
behavior they exhibit today, legally.

The proposed text is as follows:-

| 15.3.4.2 Function.prototype.toString ( )
|
| An implementation-dependent representation of the
| function is returned. This representation has the
| syntax of a Function Definition. Note in particular
| that the use and placement of white space, line
| terminators, and semicolons within the representation
| string is implementation-dependent.

Garrett


More information about the es-discuss mailing list