static variables?

Discussion in 'Javascript' started by jab3, Mar 1, 2006.

  1. jab3

    jab3 Guest

    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
    jab3, Mar 1, 2006
    #1
    1. Advertising

  2. jab3

    Randy Webb Guest

    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.
    }
    }

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Mar 1, 2006
    #2
    1. Advertising

  3. jab3

    VK Guest

    jab3 wrote:
    > 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.
    VK, Mar 1, 2006
    #3
  4. jab3

    VK Guest

    VK wrote:
    > 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
    VK, Mar 1, 2006
    #4
  5. jab3

    Matt Kruse Guest

    VK wrote:
    > 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>


    --
    Matt Kruse
    http://www.JavascriptToolbox.com
    http://www.AjaxToolbox.com
    Matt Kruse, Mar 1, 2006
    #5
  6. "jab3" <> writes:

    > 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
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Mar 1, 2006
    #6
  7. 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?

    Mike

    --
    Michael Winter
    Prefix subject with [News] before replying by e-mail.
    Michael Winter, Mar 1, 2006
    #7
  8. Michael Winter <> writes:

    > 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 -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Mar 1, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    933
  2. SaravanaKumar
    Replies:
    6
    Views:
    9,395
    Tony Morris
    Oct 19, 2004
  3. Neviton
    Replies:
    9
    Views:
    456
    Alf P. Steinbach
    Sep 17, 2007
  4. Martin Wells

    Static inline functions with static local variables

    Martin Wells, Oct 6, 2007, in forum: C Programming
    Replies:
    10
    Views:
    703
    Army1987
    Oct 8, 2007
  5. Hicham Mouline
    Replies:
    5
    Views:
    2,366
    James Kanze
    Dec 19, 2008
Loading...

Share This Page