Closed Bug 434912 Opened 16 years ago Closed 10 years ago

Curly braces (block) cause function not to be defined (hoisted)

Categories

(Core :: JavaScript Engine, defect)

defect
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 1071877

People

(Reporter: jamesjren, Unassigned)

References

()

Details

(Whiteboard: DUPEME)

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14
Build Identifier: 

{
  f();
  function f() {
    document.body.innerHTML = 'PASS';
  }
}

The outer curly braces cause an exception to be thrown (f is not defined) in Firefox. IE, Safari, and Opera run f.

Reproducible: Always

Steps to Reproduce:
Go to http://xenon.stanford.edu/~jamesren/tests/reorder-test-a.html
Actual Results:  
FAIL: f is not defined

Expected Results:  
PASS

Removing the curly braces allows all browsers to run f:
http://xenon.stanford.edu/~jamesren/tests/reorder-test-b.html
I can reproduce this in Firefox 3 RC1 as well.
Status: UNCONFIRMED → NEW
Ever confirmed: true
Invalid as a bug based on ECMA standards, since ECMA-262 Ed. 3 does not specify function definitions as sub-statements -- only as expressions and at top-level. The extension in SpiderMonkey is old and it serves to satisfy the conditional compilation use-case:

if (DEBUG) {
  function maybe() {...}
}

if DEBUG evaluates to falsy, no function maybe is defined. More, this means the binding of maybe depends on evaluation of the sub-statement containing it. Same goes for the testcase in comment 0.

ECMA-262 Ed. 3 (chapter 16) allows syntactic extensions, which the function as sub-statement definitely is an example of. That we differ from other browsers is a tolerable consequence of ES3 chapter 16 allowing syntactic extension. Note that for ES4, function in a block will be block-scoped, but hoiste to top of the block, so the testcase in comment 0 will work.

/be
Whiteboard: DUPEME
Summary: Curly braces cause function not to be defined → Curly braces (block) cause function not to be defined (hoisted)
So is this INVALID? OR a dupe of bug 585536, bug 561670, bug 593176?
Assignee: general → nobody
Forward-duping to where you will actually get what you ask for (function hoisting in blocks, albeit only in strict mode): bug 1071877.
Status: NEW → RESOLVED
Closed: 10 years ago
Resolution: --- → DUPLICATE
(In reply to :Gijs Kruitbosch from comment #8)
> Forward-duping to where you will actually get what you ask for (function
> hoisting in blocks, albeit only in strict mode): bug 1071877.
> 
> *** This bug has been marked as a duplicate of bug 1071877 ***

Are you sure? Bug 1071646 sounds like a better match to me as bug 1071877 doesn't plan to hoist it out of the block.
You need to log in before you can comment on or make changes to this bug.