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 is not a bug.
A semicolon clearly can't be inserted here because it would change the
meaning of a valid program
I meant to say "of a syntactically valid program".
-- as opposed to making an invalid program
valid as in other cases of semicolon insertion. (Nor should we even
be considering any extensions to semicolon insertion, which is and
always was simply a bad idea.)
The program also can't be made invalid
syntactically invalid
David-Sarah Hopwood wrote:
> Michael Daumling wrote:
>> 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 is not a bug.
>
> A semicolon clearly can't be inserted here because it would change the
> meaning of a valid program
I meant to say "of a syntactically valid program".
> -- as opposed to making an invalid program
> valid as in other cases of semicolon insertion. (Nor should we even
> be considering *any* extensions to semicolon insertion, which is and
> always was simply a bad idea.)
>
> The program also can't be made invalid
syntactically invalid
> because that would be a backward-incompatible change, and because there
> is no reason to make it invalid.
--
David-Sarah Hopwood
David-Sarah Hopwood wrote:
I meant to say "of a syntactically valid program".
syntactically invalid