Closed Bug 762365 Opened 14 years ago Closed 14 years ago

JS function unexpectedly executing without explicit call

Categories

(Core :: JavaScript Engine, defect)

x86_64
Linux
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: WeirdAl, Unassigned)

Details

(Keywords: testcase)

Attachments

(2 files)

Attached file testcase —
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?
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?
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
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?
I'm starting to see it now... but dang, that's weird. :)
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.
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.

Attachment

General

Created:
Updated:
Size: