473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Single closure and multiple closure.

dmjpro
2,476 2GB
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
3 1071
rnd me
427 Expert 256MB
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
2,476 2GB
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
2,476 2GB
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

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

Similar topics

27
by: Ted Lilley | last post by:
What I want to do is pre-load functions with arguments by iterating through a list like so: >>>class myclass: .... pass >>>def func(self, arg): .... print arg >>>mylist = >>>for item...
1
by: Victor Ng | last post by:
Is there a way to preserve the argspec of a function after wrapping it in a closure? I'm looking for a general way to say "wrap function F in a closure", such that inspect.getargspec on the...
9
by: Mikito Harakiri | last post by:
Transitive closure (TC) of a graph is with TransClosedEdges (tail, head) as ( select tail, head from Edges union all select e.tail, ee.head from Edges e, TransClosedEdges ee where e.head =...
2
by: Matt Kruse | last post by:
Let's say I want to create two (or more) global functions from within a closure, so they can both share some "private" variables and methods. What is the recommended way to do this? (function(){...
7
by: Csaba Gabor | last post by:
I feel like it's the twilight zone here as several seemingly trivial questions are bugging me. The first of the following three lines is a syntax error, while the last one is the only one that...
9
by: User1014 | last post by:
I'm a javascript noob and have a question concerning some code I've come across that I'm trying to understand. Here is a snippet of the relevant section: (snip) var closure = this; var xhr =...
0
by: Gerard Brunick | last post by:
Consider: ### Function closure example def outer(s): .... def inner(): .... print s .... return inner .... 5
11
by: Huayang Xia | last post by:
What will the following piece of code print? (10 or 15) def testClosure(maxIndex) : def closureTest(): return maxIndex maxIndex += 5 return closureTest()
25
by: Darsin | last post by:
Hi all I need to perform a summation on a column of a table based on a criteria given in another column in the same table. The catch is i need to perform different sums according to the number of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.