Description: The specification of Catch is clear enough, but has an unfortunate consequence.

Catch is defined as creating a new object with the caught object as a property and putting the new object at the head of the scope chain.

If the caught object is a function, calling it within the catch supplies the head of the scope chain as the this value. The called function can add properties to this object. This implies that for code of this shape:

    var x;
    try { ...}
    catch (E)
    {
       E();
       return x;
    }

The reference to x within the catch is not necessarily to the local declaration of x;

For example:

function foo() {
  this.x = 11;
}
 
x = "global.x";

try {
  throw foo;
} catch(e) {
  print(x) // Should print "global.x"
  e();
  print(x) // Should add x to e
           // (Both IE and Firefox modify the global x)
}

print(x);  // Should print "global.x". IE and Firefox both print 11

This gives Catch the same performance problems as with. If the call to E above were specified to supply the global object (rather than the head of the scope chain) as the this value, the performance issue evaporates and the catch variable can be treated as a local scoped to the catch clause.

Relevant Goal: 1
Relevant Design Principle: 2a, 2b
Relevant ES4 proposal: bug fixes

Pratap Lakshman 2007/04/15 03:17

FYI, Firefox 1.5 and earlier followed ES3. Firefox 2 anticipates ES4 based on IE‘s precedent.

Brendan Eich 2007/04/16 17:46

 
es3.1/catch_clause_context_specification.txt · Last modified: 2008/03/30 16:24 by markm
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki