0 Replies Latest reply on Oct 13, 2009 8:44 AM by Marc Autret

    [JS] ID CS3 Closure Bug (?)

    Marc Autret Level 4

      Hi folks,

       

      trying to get a CS4 script compatible with ID CS3 (), I discover today a deeppitfall relating to variable scopes and closures.

       

      It appears that ID CS3 may unexpectedly lost the access to a "closed" local variable when an outer variable shares the same name with it. I say "unexpectedly" because it's not the way JavaScript is supposed to work. The confusing fact is that this bug (?) is fixed in CS4, so the same code has two behaviors according to the version:

       

      var outerFct = (function()
           {
           var v = 'v in outerFct';
      
           var innerFct = (function()
                {
                var v = 'v in innerFct'; // v hides locally outer-v
                var w = 'w in innerFct'; // no conflict with any outer var
                
                return function() { alert(v + ' - ' + w); }
                })();
      
           innerFct();
           })();
           
      // Results:
      
      // ID CS4 alerts 'v in innerFct - w in innerFct' (as expected)
      // ID CS3 alerts 'v in outerFct - w in innerFct' (!!!)
      

       

      Thus, when you create a closure and need to invoke a local variable from a local function, be aware of outer variable names (if you script for CS3).

       

      Let me know if you see the same things on your OS.

       

      @+

      Marc