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

Re: scope bug? catch (e) "e already defined"

Expand Messages
  • Douglas Crockford
    ... There are scope definition problems with respect to catch in some browsers. I recommend that you give each catch its own name (e1, e2) to be safe.
    Message 1 of 7 , Jun 16, 2008
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "wleingang" <wleingang@...> wrote:
      >
      > Hi. Has anyone run into this before?
      >
      > try {
      > this.var1 = "test";
      > }catch (e) {}
      >
      > try {
      > this.var2 = "test";
      > }catch (e) {}
      >
      >
      > On the second catch, JSLint thinks that e is already defined... even
      > though from that scope there is no way it is already defined. Unless I
      > don't understand this should be valid code right?

      There are scope definition problems with respect to catch in some
      browsers. I recommend that you give each catch its own name (e1, e2)
      to be safe.
    • Re Miya
      ... Unless I ... This is what I have done. Use different names for the events. However I have stuck into another problem: var test = function(){
      Message 2 of 7 , Jun 16, 2008
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...>
        wrote:
        >
        > --- In jslint_com@yahoogroups.com, "wleingang" <wleingang@> wrote:
        > >
        > > Hi. Has anyone run into this before?
        > >
        > > try {
        > > this.var1 = "test";
        > > }catch (e) {}
        > >
        > > try {
        > > this.var2 = "test";
        > > }catch (e) {}
        > >
        > >
        > > On the second catch, JSLint thinks that e is already defined... even
        > > though from that scope there is no way it is already defined.
        Unless I
        > > don't understand this should be valid code right?
        >
        > There are scope definition problems with respect to catch in some
        > browsers. I recommend that you give each catch its own name (e1, e2)
        > to be safe.
        >
        This is what I have done. Use different names for the events.

        However I have stuck into another problem:

        var test = function(){
        this.pub_function = function(){
        pvt_fn();
        }
        var pvt_fn = function(){
        // Code here
        }
        }

        JsLint reports pvt_fn as a global reference. Is this normal?
      • Douglas Crockford
        ... Move the definition of pvt_fn before the pub_function that uses it.
        Message 3 of 7 , Jun 16, 2008
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, "Re Miya" <remiya_ws@...> wrote:
          > However I have stuck into another problem:
          >
          > var test = function(){
          > this.pub_function = function(){
          > pvt_fn();
          > }
          > var pvt_fn = function(){
          > // Code here
          > }
          > }
          >
          > JsLint reports pvt_fn as a global reference. Is this normal?

          Move the definition of pvt_fn before the pub_function that uses it.
        • Re Miya
          ... That really makes sense. Up to now I have been putting the private fns at the end like this var test = function(){ this.pub_function = function(){
          Message 4 of 7 , Jun 16, 2008
          • 0 Attachment
            --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...>
            wrote:
            >
            > --- In jslint_com@yahoogroups.com, "Re Miya" <remiya_ws@> wrote:
            > > However I have stuck into another problem:
            > >
            > > var test = function(){
            > > this.pub_function = function(){
            > > pvt_fn();
            > > }
            > > var pvt_fn = function(){
            > > // Code here
            > > }
            > > }
            > >
            > > JsLint reports pvt_fn as a global reference. Is this normal?
            >
            > Move the definition of pvt_fn before the pub_function that uses it.
            >
            That really makes sense.
            Up to now I have been putting the private fns at the end like this
            var test = function(){
            this.pub_function = function(){
            pvt_fn();
            }
            //------------ PRIVATE METHODS -----------//
            var pvt_fn = function(){
            // Code here
            }
            }
            Now I will put them at the top

            var test = function(){
            //------------ PRIVATE METHODS -----------//
            var pvt_fn = function(){
            // Code here
            }

            //------------ PUBLIC METHODS -----------//
            this.pub_function = function(){
            pvt_fn();
            }
            }

            Thank you!
          Your message has been successfully submitted and would be delivered to recipients shortly.