Loading ...
Sorry, an error occurred while loading the content.

Re: catch (e) ... 'e' is already defined !?!

Expand Messages
  • boyopeg
    They are both in the same scope. Look at this function test() { try { throw test ; } catch (error) { } alert(error); }
    Message 1 of 5 , Aug 10, 2010
    • 0 Attachment
      They are both in the same scope. Look at this

      function test() {

      try {
      throw "test";
      }
      catch (error) { }

      alert(error);

      }

      --- In jslint_com@yahoogroups.com, "Alexandre Morgaut" <morgaut@...> wrote:
      >
      > in this sample of code:
      > --------------------
      > function test() {
      > var foo;
      >
      > try {
      > foo += 1;
      > } catch (error) {}
      >
      > try {
      > foo += 1;
      > } catch (error) {
      > foo -= 1;
      > }
      >
      > }
      > --------------------
      >
      >
      > JSLint returns me:
      > --------------------
      > Error:
      > Problem at line 10 character 14: 'error' is already defined.
      >
      > } catch (error) {
      > --------------------
      >
      >
      > Well, the previously "defined" error was not in the same catch scope... so what ??
      >
    • Alexandre Morgaut
      Well it shouldn t On which browser did you test it ? On Firefox - this generate a ReferenceError: error is not defined On Safari: - this generate also a
      Message 2 of 5 , Aug 11, 2010
      • 0 Attachment
        Well it shouldn't
        On which browser did you test it ?

        On Firefox
        - this generate a ReferenceError: "error is not defined"

        On Safari:
        - this generate also a ReferenceError: Can't find variable: error

        which is good

        So bad if IE is different
        But maybe older versions of Firefox and Safari had the same problem

        Well if some browers doesn't support the catch scope correctly, I understand why JSLint warn about it. I would just have liked a better message as from the ECMAScript level 3 perspective the scope are different
        ...
        6: Remove Result(2) from the scope chain...
        ...
        (where Result(2) is the catch block parameter)

        --- In jslint_com@yahoogroups.com, "boyopeg" <boyopeg@...> wrote:
        >
        > They are both in the same scope. Look at this
        >
        > function test() {
        >
        > try {
        > throw "test";
        > }
        > catch (error) { }
        >
        > alert(error);
        >
        > }
        >
      • Jean-Charles Meyrignac
        ... What boyopeg wanted to say is that Javascript has only a limited scope. In your original message, catch(error) generates multiple definitions in the
        Message 3 of 5 , Aug 11, 2010
        • 0 Attachment
          >
          > On Wed, Aug 11, 2010 at 11:06 AM, Alexandre Morgaut <morgaut@...>wrote:
          >


          What boyopeg wanted to say is that Javascript has only a limited scope.
          In your original message, catch(error) generates multiple definitions in the
          routine's scope (at least in IE).

          For example:

          function func1(test)
          {
          var a = 1;
          if (test)
          {
          var a = 2;
          var b;
          }
          alert(a);
          }

          The second 'var a' is the same as the first one, 'func1(true)' will display
          '2' and you'll also have a 'b' variable declared at the end of the routine.

          This is why we have ugly code for handling events in JS, because IE doesn't
          send the parent event on onclick for example:
          function onclickedobject(evt)
          {
          if (!evt) var evt = window.event; // just for IE, isn't that ugly ?
          }

          I also saw terrible code, where people didn't use the var keyword:
          function func()
          {
          for(i = 0;i<10;++i) // yes, this variable is global !!!
          etc...
          }
          I found these bugs when dumping the global variables.

          On Wed, Aug 11, 2010 at 11:06 AM, Alexandre Morgaut <morgaut@...>wrote:

          >
          >
          > Well it shouldn't
          > On which browser did you test it ?
          >
          > On Firefox
          > - this generate a ReferenceError: "error is not defined"
          >
          > On Safari:
          > - this generate also a ReferenceError: Can't find variable: error
          >
          > which is good
          >
          >
          You are nit-picking.
          The example boyopeg has provided is probably not the best one, since the
          behaviour is different from a browser to another.

          But anyway, we are all trying to write cross-browser code, so we ultimately
          need to consider IE's limitations.

          JC


          [Non-text portions of this message have been removed]
        • Alexandre Morgaut
          Hi Jean-Charles, You are perfectly right when you say that there is no specific scope in conditional blocks I know so far 4 kind of scopes in JavaScript: - the
          Message 4 of 5 , Aug 11, 2010
          • 0 Attachment
            Hi Jean-Charles,

            You are perfectly right when you say that there is no specific scope in conditional blocks

            I know so far 4 kind of scopes in JavaScript:
            - the global scope
            - the function scope
            - the with scope
            - & the catch scope

            "with" creates a specific scope in which all properties of an object are directly accessible... It highly discouraged to use it...

            the "catch" block requires an "identifier" set as a parameter for a function, create an object in matching to this exception "identifier" and remove it at the and of the catch block

            Maybe Douglas would have explained it another way but the idea is there

            So my surprise was to see that some User-Agent didn't implement this ECMAScript specificity as expected when ECMAScript level 3 exists from so far away

            So please Microsoft.. update JScript... Do IE8 and IE9 behave that quirky way too ?
          Your message has been successfully submitted and would be delivered to recipients shortly.