Closed Bug 105886 Opened 25 years ago Closed 25 years ago

[RFE] Scope JS code blocks within functions

Categories

(Core :: JavaScript Engine, enhancement)

enhancement
Not set
normal

Tracking

()

VERIFIED INVALID

People

(Reporter: matt, Assigned: rogerl)

Details

Different functions in JS seem to have their own scopes, as variables declared in one function don't set off a JS strict warning if they are defined in another function further along. However, different code blocks within a function seem to share the same scope. For instance, if you have: function abc(x) { if (x == 0) { var foo = 0; } else { var foo = 1; } } This gets you a redeclaration warning, but in C/C++/Perl/etc the different code blocks would have separate scopes, and there would be no warnring/error for code like this. Each code block should have it's own scope, so that when people use C like coding practices, it won't generate any warnings.
Severity: normal → enhancement
Um. I think the ECMA specification states that JS has function scope only. So doing what you would propose would in fact violate the Javascript standard.
Have to mark this one as invalid. The ECMA-262 specification for EcmaScript states in Chapter 10, "Execution Contexts", that there are only three types of code contexts: 1. Global code 2. Eval code (e.g. eval('some string')) 3. Function code There is no block scope in the language specification. For a good example of another bug marked invalid for this reason, see bug 55735: "strict var-redecl warning doesn't grok conditional declarations", where Brendan commented on this -
Status: NEW → RESOLVED
Closed: 25 years ago
Resolution: --- → INVALID
Marking Verified. Matthew, thank you for this report. The point is well-taken. You will be interested to see the spec for JavaScript 2.0 (and EcmaScript 4). This contains a provision for block scope in this next generation of the language - See http://www.mozilla.org/js/language/js20.html
Status: RESOLVED → VERIFIED
P.S. - you can see the EcmaScript 3 spec at http://www.mozilla.org/js/language/E262-3.pdf
You need to log in before you can comment on or make changes to this bug.