Single closure and multiple closure.

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

    Single closure and multiple closure.

    Hey experts,
    i need two examples with little bit explanation ...
    when only one closure created and when multiple closures created per each function call when a function reference returned from a outer function.

    I think you got my points .....
  • rnd me
    Recognized Expert Contributor
    • Jun 2007
    • 427

    #2
    are you talking about a private method?


    Code:
    function adder(base){
       var amt = Number(base);
       var total = amt;
      function add(){
        total += amt;
        return total;
      }
    return add;
    }
    
    //test"
    a= new adder(6)
    a()//12
    a()//18
    a()//24
    the running total and the increment amount are closed by the inner function.

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by rnd me
      are you talking about a private method?


      Code:
      function adder(base){
         var amt = Number(base);
         var total = amt;
        function add(){
          total += amt;
          return total;
        }
      return add;
      }
      
      //test"
      a= new adder(6)
      a()//12
      a()//18
      a()//24
      the running total and the increment amount are closed by the inner function.

      May be i was wrong ... i could not explain what i wanted to know :-(
      I tested this morning to explain you a little bit clear .
      My code snippet goes here ...

      [code=javascript]
      function outer_function( )
      {
      var a = 10;
      function inner_function( ){
      a++;
      alert(a);
      }
      return inner_function;
      }

      function test(){
      var a = outer_function( );
      for(var i=0;i<10;i++) a();
      }
      [/code]

      And i call the test function some how, and what i got that is a single closure is shared by each function call ..means each inner function calls.
      But i wanted to know that can it be possible 2 create separate closure for each inner function calls.
      I think you are confused to understand ..
      let me be clear more ...Yesterday i went through a site ..
      go through the 7th example
      please make me understand :-)

      Kind regards ...

      Comment

      • dmjpro
        Top Contributor
        • Jan 2007
        • 2476

        #4
        Originally posted by dmjpro
        May be i was wrong ... i could not explain what i wanted to know :-(
        I tested this morning to explain you a little bit clear .
        My code snippet goes here ...

        [code=javascript]
        function outer_function( )
        {
        var a = 10;
        function inner_function( ){
        a++;
        alert(a);
        }
        return inner_function;
        }

        function test(){
        var a = outer_function( );
        for(var i=0;i<10;i++) a();
        }
        [/code]

        And i call the test function some how, and what i got that is a single closure is shared by each function call ..means each inner function calls.
        But i wanted to know that can it be possible 2 create separate closure for each inner function calls.
        I think you are confused to understand ..
        let me be clear more ...Yesterday i went through a site ..
        go through the 7th example
        please make me understand :-)

        Kind regards ...

        Sorry is that .... each closure created for each outer function call? :-)
        Sorry!
        Anyway thanks all for your kind help.

        Comment

        Working...