HighDots Forums  

static variables?

Javascript JavaScript language (comp.lang.javascript)


Discuss static variables? in the Javascript forum.



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

Default static variables? - 03-01-2006 , 02:21 AM






Does JavaScript have "static" variables. That is, as in C (or local in
Perl)? How can I keep a variable in a JavaScript function that doesn't
change from call to call? It may not make sense in JavaScript; I'm not
sure when the variables are re-set, but I assume it's at each page full
reload. But, what if I want to validate a form, and force a user to
re-enter something I find invalid, but only do it the first 1 or 2
times. Then, I will let them out, so as not to lock them into a field.
Must I use standard global variables for this, or does JavaScript have
a static variable that doesn't involve object-oriented framework?


Thanks,
jab3


Reply With Quote
  #2  
Old   
Randy Webb
 
Posts: n/a

Default Re: static variables? - 03-01-2006 , 03:34 AM






jab3 said the following on 3/1/2006 2:21 AM:
Quote:
Does JavaScript have "static" variables. That is, as in C (or local in Perl)?
No, it has no concept of static variables.

Quote:
How can I keep a variable in a JavaScript function that doesn't
change from call to call? It may not make sense in JavaScript; I'm not
sure when the variables are re-set, but I assume it's at each page full
reload.
Depends on where the variable is defined when it is "reset".

Quote:
But, what if I want to validate a form, and force a user to
re-enter something I find invalid, but only do it the first 1 or 2
times. Then, I will let them out, so as not to lock them into a field.
Use a global variable and every time they get it wrong, increment your
global counter. When it reaches the max tries, ignore it:

var counter = 0;

function validateForm(){
if (counter<3){
//validation code here.
}
}

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Reply With Quote
  #3  
Old   
VK
 
Posts: n/a

Default Re: static variables? - 03-01-2006 , 04:02 AM




jab3 wrote:
Quote:
Does JavaScript have "static" variables. That is, as in C (or local in
Perl)?
'static' in JavaScript has rather C++ sense: "a single instance of
something shared among all instances of the given constructor".

If you're talking about Perl-like 'local' (thus "visible to the given
function and all functions called from that function") then this scope
doesn't exists in JavaScript - though it can be emulated. But you seem
do not need it for this particular task (?)

Quote:
How can I keep a variable in a JavaScript function that doesn't
change from call to call?
By making such variable global or by making it a property of some
persistent object.

Quote:
It may not make sense in JavaScript; I'm not
sure when the variables are re-set, but I assume it's at each page full
reload.
On full reload the whole global context is being re-initialized,
including any variables - global or local. To keep states between page
loads you need then either use cookies or the search part of URL, or
hidden form fields.

Quote:
But, what if I want to validate a form, and force a user to
re-enter something I find invalid, but only do it the first 1 or 2
times. Then, I will let them out, so as not to lock them into a field.
Must I use standard global variables for this, or does JavaScript have
a static variable that doesn't involve object-oriented framework?
Unless there are some extras in your situations, I would go with a
global var.



Reply With Quote
  #4  
Old   
VK
 
Posts: n/a

Default Re: static variables? - 03-01-2006 , 04:11 AM




VK wrote:
Quote:
Unless there are some extras in your situations, I would go with a
global var.
Just donged on me ! :-)
You must want a separate counter for each form filed, this is why all
these local issues. In such case you can add extra property to the form
element itself:

function validate(elm) {
// elm is a form element reference
if ('undefined' == elm.counter) {
elm.counter = 0;
}
else {
elm.counter++;
}
if (elm.counter <= 2) {
// be nasty
}
else {
// let it go
}
}

P.S. You are welcome to compact the above



Reply With Quote
  #5  
Old   
Lasse Reichstein Nielsen
 
Posts: n/a

Default Re: static variables? - 03-01-2006 , 01:46 PM



"jab3" <jab3.42 (AT) gmail (DOT) com> writes:

Quote:
Does JavaScript have "static" variables. That is, as in C (or local in
Perl)? How can I keep a variable in a JavaScript function that doesn't
change from call to call?
Not directly, no.
It's not necessary either, since Javascript has block scoping and
closures, so you can create a variable that is only visible inside a
function without doing it inside the function:

var myFunc = (function(){
var myStaticVariable = 0;
return function myFunc() {
return myStaticVariable++;
}
})();

That's really all static variables are in C anyway: globally stored
variables with local scope.

Quote:
It may not make sense in JavaScript; I'm not sure when the variables
are re-set, but I assume it's at each page full reload.
When you load a page, everything goes away. You must use cookies or
pass information in the URL to keep state from one page to the next.

Quote:
But, what if I want to validate a form, and force a user to re-enter
something I find invalid, but only do it the first 1 or 2 times.
That sounds like state that should be kept somewhere, and not
necessarily hidden inside a function. It makes the function impossible
to reuse if it has state hardwired into it.

/L
--
Lasse Reichstein Nielsen - lrn (AT) hotpop (DOT) com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


Reply With Quote
  #6  
Old   
Michael Winter
 
Posts: n/a

Default Re: static variables? - 03-01-2006 , 02:14 PM



On 01/03/2006 18:46, Lasse Reichstein Nielsen wrote:

Quote:
[...] Javascript has block scoping and closures [...]
The latter certainly, but block scoping? Surely you mean function-local
variables?

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.


Reply With Quote
  #7  
Old   
Lasse Reichstein Nielsen
 
Posts: n/a

Default Re: static variables? - 03-01-2006 , 02:30 PM



Michael Winter <m.winter (AT) blueyonder (DOT) co.uk> writes:

Quote:
On 01/03/2006 18:46, Lasse Reichstein Nielsen wrote:

[...] Javascript has block scoping and closures [...]

The latter certainly, but block scoping? Surely you mean
function-local variables?
Indeed. I got carried away there

/L
--
Lasse Reichstein Nielsen - lrn (AT) hotpop (DOT) com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


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.