Closed
Bug 762365
Opened 14 years ago
Closed 14 years ago
JS function unexpectedly executing without explicit call
Categories
(Core :: JavaScript Engine, defect)
Tracking
()
RESOLVED
INVALID
People
(Reporter: WeirdAl, Unassigned)
Details
(Keywords: testcase)
Attachments
(2 files)
var X = {};
X.Exn = function() { throw new Error("Not reached!"); }
(function() {})();
/*
Exception: Not reached!
((function () {}))@Scratchpad:2
@Scratchpad:3
*/
Why are we executing X.Exn right away?
| Reporter | ||
Comment 1•14 years ago
|
||
This testcase passes. Note the semicolon at the end of line 2.
var X = {};
X.Exn = function() { throw new Error("Not reached!"); };
(function() {})();
How am I misunderstanding the ECMAScript standard this time?
| Reporter | ||
Comment 2•14 years ago
|
||
Comment 3•14 years ago
|
||
Without the semicolon, the thing the JS parser sees is:
X.Exn = function() { throw new Error("Not reached!"); } (function() {}) ();
This is syntactically valid JS, as far as I can tell: it's a function call, where the function is the return value of the expression:
X.Exn = function() { throw new Error("Not reached!"); }
and the function argument is the expression:
function() {}
and then the return value of that function call is itself called as a function with no arguments.
The return value of an assignment expression is the RHS, so you get the observed behavior.
Since the syntax is valid, no ASI (which is a syntax-error-fixup mechanism) takes place.
Status: NEW → RESOLVED
Closed: 14 years ago
Resolution: --- → INVALID
| Reporter | ||
Comment 4•14 years ago
|
||
I'm sorry, Boris, but I just don't follow that, especially since what the JS parser saw was on two different lines. Where in the ECMAScript specification does it state that the continuation is allowed without a backslash to escape the newline?
| Reporter | ||
Comment 5•14 years ago
|
||
I'm starting to see it now... but dang, that's weird. :)
Comment 6•14 years ago
|
||
JS doesn't attach special meaning to newlines unless ASI is taking place (which only happens in a limited range of cases).
This is why in JS this works:
x.
Exn = function() {};
as does this:
x
.Exn = function() {};
and why in JS this returns void:
return
5;
Basically, in JS a newline is NOT a statement separator except in a few very special cases.
Comment 7•14 years ago
|
||
I should have been clearer: my third example above _is_ a case where ASI explicitly happens.
You might want to read section 7.9.1 of ES5 for the details.
You need to log in
before you can comment on or make changes to this bug.
Description
•