Closed Bug 417730 Opened 16 years ago Closed 16 years ago

Block scope in JavaScript will affect the visibility of variables

Categories

(Core :: JavaScript Engine, defect)

x86
Windows Server 2003
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: yudi, Unassigned)

Details

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.2; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12

code1:
alert(fn1()); 
function fn1(){ return "fn1"; }

code2:
{
    alert(fn1()); 
    function fn1(){ return "fn1"; }
}

code1 works, but code2 will throw an exception
both code1 and code2 works correctly in IE, Opera and Safari

Reproducible: Always

Actual Results:  
both code1 works, but code2 throw an exception

Expected Results:  
both code1 and code2 run correctly in JavaScript
Assignee: nobody → general
Component: General → JavaScript Engine
Product: Firefox → Core
QA Contact: general → general
Sorry, ECMA-262 does not specify functions as statements (nested under other statements, including blocks), and SpiderMonkey forever has implemented them as an extension that binds the function name if and only if control flow reaches the function statement. This is useful for conditional compilation:

if (DEBUG) {
  function dbg_helper(a, b, c) {...}
  ...
}

We are not going to change to match IE, which oddly always defines the function even if control flow does not reach its definition. I did not know that Safari (JavaScriptCore) had cloned IE in this regard, although via ECMA TG1 (now TC39) we knew Opera had.

Sticking with INVALID for now, could go with WONTFIX, may be forced to emulate IE at some point but taking a stand here. See the thread starting at "Function inside if statement" at https://mail.mozilla.org/pipermail/es4-discuss/2008-February/thread.html from the ES4 discussion list. One of the messages here refers to the thread from last year on the same topic at https://mail.mozilla.org/pipermail/es4-discuss/2007-March/000483.html.

/be
Status: UNCONFIRMED → RESOLVED
Closed: 16 years ago
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.