static variables?

J

jab3

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
 
R

Randy Webb

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

No, it has no concept of static variables.
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".
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.
}
}
 
V

VK

jab3 said:
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 (?)
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.
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.
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.
 
V

VK

VK said:
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
 
M

Matt Kruse

VK said:
Unless there are some extras in your situations, I would go with a
global var.

Global vars should be avoided when possible (a 'best practice' I'll add to
my document).

You can achieve the effect of a "static variable" using closures as below.
The function should, of course, be expanded to be more useful than it is
written.

<html>
<head>
<title>Example</title>
<script type="text/javascript">
var validateField = (function(){
var count = 0;
return function(o) {
if (count++<2 && o.value=="") {
alert("Field cannot be empty!");
return false;
}
return true;
}
})();
</script>
</head>
<body>

<form action="/" method="get">
<input type="text" name="field" value="">
<input type="button" onClick="validateField(this.form.field)"
value="Validate">
</form>

</body>
</html>
 
L

Lasse Reichstein Nielsen

jab3 said:
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.
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.
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top