|
CatchBlocksScopeBleed
catch blocks don't always introduce a new scope.
catch blocks may cause global assignment, or local scope creepEffectReplace global variables. BackgroundEcmaScript 262 says that only the Program and FunctionDeclaration constructor introduce new lexical scopes. EcmaScript 262 section 10.1.4 says During execution within an execution context, the scope chain of the execution context is affected only by with statements (see 12.10) and catch clauses (see 12.14). Section 12.10 describes with statements: The with statement adds a computed object to the front of the scope chain of the current execution context, then executes a statement with this augmented scope chain, then restores the scope chain. Section 12.14 describes catch clauses The production Catch : catch (Identifier ) Block is evaluated as follows:
Existing interpreters fail to implement the semantics of 12.14. Old versions of firefox allow global assignment this way. IE introduces it as a variable in the local scope, instead of creating a new scope (see bug 1032). AssumptionsThere is no local variable of the same name as the caught variable already in scope, or there is one, and that variable is referenceable outside scope. VersionsInconsistent Examplevar a = 0;
(function () {
try {
throw 1;
} catch (a) {
}
})();
alert(a); // alerts 1 on old FF
(function () {
var a = 0;
try {
throw 1;
} catch (a) {
}
alert(a); // alerts 1 on IE 6 and 7 (IE 8 not tested)
})();
|
Sign in to add a comment
"Existing interpreters fail to implement the semantics of 12.14. Old versions of firefox allow global assignment this way."
How old? Anyone know the bug number, or have an example of code that triggered this bug?