The interesting question on Closure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    The interesting question on Closure

    Today i looked at JavaScript closures. There i came across a thing closure comes in when an inner function defined inside an outer function. So when outer function exits and returns the inner function reference then a hidden reference returns along with the reference of inner function.

    Now my question is when that hidden reference created? And the hidden reference have the references of all local variables of outer function. Now my second question..
    If any variables local to a loop scope are also being referenced by hidden pointer. But those variables get destroyed after loop scope ends up, how it gets handled?

    Now have a look at code snippet....
    [code=javascript]
    function outer(){
    var var1 = some_value;
    var inner = function(){some _code;}
    return inner;
    }
    [/code]

    Here when inner returns then a closure of outer's local variables get created.
    so the reference of inner variable being referenced by hidden pointer or only variable var1?

    I think experts can get my point of view.
    Please answer these questions!
  • rnd me
    Recognized Expert Contributor
    • Jun 2007
    • 427

    #2
    Originally posted by dmjpro
    Today i looked at JavaScript closures. There i came across a thing closure comes in when an inner function defined inside an outer function. So when outer function exits and returns the inner function reference then a hidden reference returns along with the reference of inner function.
    1. loops don't have scope in javascript.
    2. your example function does not produce a closure, no variable name is reused to be preserved.


    here is a simple closure example, showing how functions preserve scope.

    Code:
        var name = "bob";
    
      function demoClosures(){
        var name = "fred";
        setTimeout(  "alert('not closed: hello '     +   name )"  , 100);
        setTimeout(  function(){ alert('closed: hello '+ name )} , 500);
      }
    
      demoClosures();
    notice how name is (en)closed in the function-based setTimeout.

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by rnd me
      1. loops don't have scope in javascript.
      2. your example function does not produce a closure, no variable name is reused to be preserved.


      here is a simple closure example, showing how functions preserve scope.

      Code:
          var name = "bob";
      
        function demoClosures(){
          var name = "fred";
          setTimeout(  "alert('not closed: hello '     +   name )"  , 100);
          setTimeout(  function(){ alert('closed: hello '+ name )} , 500);
        }
      
        demoClosures();
      notice how name is (en)closed in the function-based setTimeout.
      OK i checked that there is no scope inside a function .... :-)
      Actually i didn't mention that the variable used by my inner function code.
      Anyway i can understand and thanks for sharing your knowledge.
      Now my question is that which name will be used in inner function; the global one or the enclosed name?
      Please explain...

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5388

        #4
        in your first timeout the construct is evaled later out of the scope of the current function from where it is called so it will alert 'bob', which is your global variable ... the second one closures the value of the local variable name and will alert 'fred' ... ok? :)

        Comment

        • rnd me
          Recognized Expert Contributor
          • Jun 2007
          • 427

          #5
          Originally posted by dmjpro
          OK i checked that there is no scope inside a function .... :-)
          you should check again. functions are the about only thing that has scope in javascript. 1. 2. 3.


          to answer your question though, the closest, most-local, variable will be used. it will creep outward looking for matches of the name until get to window (global). .prototype props and methods are checked only if no other match is found.

          Comment

          Working...