HighDots Forums  

Lifetime of a functions private variables

JavaScript discussion (multi-lingual) JavaScript discussion (alt.comp.lang.javascript)


Discuss Lifetime of a functions private variables in the JavaScript discussion (multi-lingual) forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Johnny Nielsen
 
Posts: n/a

Default Lifetime of a functions private variables - 05-01-2006 , 06:40 PM






I have a question regarding the lifetime of a functions private variables.

From my knowledge in other languages I would expect a functions private
variables to be "unreachable" as soon as the function has finished executing
(which in some other languages would pop off the variables from the stack)
but it looks to me that javascript is working somewhat different.

The following code shows that the private variable "i" can be reached by the
both interval-functions after the functions scope is left - why is that ?

Is this a "dirty" way to address the variable or is it fully supported ?


function c() {
document.forms.myform.text1.value = "";
document.forms.myform.text2.value = "";
var i = 0;
var timer1 = window.setInterval(
function() {
document.forms.myform.text1.value = i;
}, 500, "javascript" );
var timer2 = window.setInterval(
function() {
document.forms.myform.text2.value = i++;
}, 1000, "javascript" );
}

<body onload="c();">
<form name="myform" action="">
<input type="text" name="text1" size="50" />
<br />
<input type="text" name="text2" size="50" />
</form>
</body>


Best regards
Johnny Nielsen



Reply With Quote
  #2  
Old   
Richard Cornford
 
Posts: n/a

Default Re: Lifetime of a functions private variables - 05-02-2006 , 06:53 AM







Johnny Nielsen wrote:
Quote:
I have a question regarding the lifetime of a functions private variables.

From my knowledge in other languages I would expect a functions private
variables to be "unreachable" as soon as the function has finished executing
(which in some other languages would pop off the variables from the stack)
but it looks to me that javascript is working somewhat different.

The following code shows that the private variable "i" can be reached by the
both interval-functions after the functions scope is left - why is that ?

Is this a "dirty" way to address the variable or is it fully supported ?
snip

Formalised in ECMA 262, 3rd Ed. and also supported (more or less) in
some preceding browser. See:-

<URL: http://jibbering.com/faq/faq_notes/closures.html >

Richard.



Reply With Quote
  #3  
Old   
Johnny Nielsen
 
Posts: n/a

Default Re: Lifetime of a functions private variables - 05-04-2006 , 12:56 PM



Thanks. From my understanding it looks like these "closures" is something
one should really be aware of when dealing with Ajax/webservice-based
"static" pages where memory usage might build up.

Best regards
Johnny Nielsen


"Richard Cornford" <Richard (AT) litotes (DOT) demon.co.uk> wrote

Quote:
Johnny Nielsen wrote:
I have a question regarding the lifetime of a functions private
variables.

From my knowledge in other languages I would expect a functions private
variables to be "unreachable" as soon as the function has finished
executing
(which in some other languages would pop off the variables from the
stack)
but it looks to me that javascript is working somewhat different.

The following code shows that the private variable "i" can be reached by
the
both interval-functions after the functions scope is left - why is that ?

Is this a "dirty" way to address the variable or is it fully supported ?
snip

Formalised in ECMA 262, 3rd Ed. and also supported (more or less) in
some preceding browser. See:-

URL: http://jibbering.com/faq/faq_notes/closures.html

Richard.




Reply With Quote
  #4  
Old   
Richard Cornford
 
Posts: n/a

Default Re: Lifetime of a functions private variables - 05-07-2006 , 11:27 AM



Johnny Nielsen wrote:
Quote:
From my understanding it looks like these "closures" is something
one should really be aware of when dealing with Ajax/webservice-based
"static" pages where memory usage might build up.
Closures are a consequence of the fundamental nature of javascript.
Nobody can reasonably clam to understand javascript without
understanding closures, when and how to use them and when and why not
to. My opinion would be that nobody should even consider touching an
area as issue-heavy as AJAX/web-services without first acquiring a
complete understanding of javascript.

Richard.



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.