Is the following a bug that needs to be fixed for ES 3.1?

Michael Daumling mdaeumli at
Thu Sep 4 01:13:05 PDT 2008

Hi all,

The following code produces a "first is not a function" error:

Array.prototype.first = function()
      { return this.length == 0 ? null : this[0]; }
// no semicolon here
(function() { if ([1,2].first() == 1) alert ("OK"); }())

If the two statements are separated via a semicolon, there is no error.

The problem is that the second statement is treated as a function argument to the function-expression. This illustrates the problem:

x=function(s) { print (s); } ('hi')

This dutifully prints 'hi'.

IMHO, there should be a rule that if a function-expression is followed by anything with brackets (), it would require the function-expression to be bracketed as well, otherwise, the bracketed statement does not become part of the assignment.

Can/should this be fixed for 3.1?


-------------- next part --------------
An HTML attachment was scrubbed...

More information about the Es-discuss mailing list