"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.'