By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,568 Members | 1,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,568 IT Pros & Developers. It's quick & easy.

Single closure and multiple closure.

dmjpro
100+
P: 2,476
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 .....
Aug 8 '08 #1
Share this Question
Share on Google+
3 Replies


rnd me
Expert 100+
P: 427
are you talking about a private method?


Expand|Select|Wrap|Line Numbers
  1. function adder(base){
  2.    var amt = Number(base);
  3.    var total = amt;
  4.   function add(){
  5.     total += amt;
  6.     return total;
  7.   }
  8. return add;
  9. }
  10.  
  11. //test"
  12. a= new adder(6)
  13. a()//12
  14. a()//18
  15. a()//24
  16.  
the running total and the increment amount are closed by the inner function.
Aug 8 '08 #2

dmjpro
100+
P: 2,476
are you talking about a private method?


Expand|Select|Wrap|Line Numbers
  1. function adder(base){
  2.    var amt = Number(base);
  3.    var total = amt;
  4.   function add(){
  5.     total += amt;
  6.     return total;
  7.   }
  8. return add;
  9. }
  10.  
  11. //test"
  12. a= new adder(6)
  13. a()//12
  14. a()//18
  15. a()//24
  16.  
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 ...

Expand|Select|Wrap|Line Numbers
  1. function outer_function()
  2.  {
  3.      var a = 10;
  4.     function inner_function(){
  5.         a++;
  6.         alert(a);
  7.     }
  8.     return inner_function;
  9.  }
  10.  
  11.  function test(){
  12.      var a = outer_function();
  13.     for(var i=0;i<10;i++) a();
  14.  }
  15.  
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 ...
Aug 9 '08 #3

dmjpro
100+
P: 2,476
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 ...

Expand|Select|Wrap|Line Numbers
  1. function outer_function()
  2.  {
  3.      var a = 10;
  4.     function inner_function(){
  5.         a++;
  6.         alert(a);
  7.     }
  8.     return inner_function;
  9.  }
  10.  
  11.  function test(){
  12.      var a = outer_function();
  13.     for(var i=0;i<10;i++) a();
  14.  }
  15.  
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.
Aug 9 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.