How to declare static variables in Javascript

You can declare “static” variables inside a function that will retain their values after returning, by declaring them as members of the function. example:

function myFunc(arg)
{
myFunc.variable=arg;
}

myFunc.variable should hold the value of arg between function calls. the reason behind this is that in Javascript, functions are considered objects. and when you declare a variable as a member of an object, it will remain with the object (in this case, the function) throughout its lifetime.

Advertisements
Post a comment or leave a trackback: Trackback URL.

Comments

  • jay  On June 1, 2011 at 3:52 pm

    how to set more than one two demensional array dynamically create and assign values?
    server1[]={‘in’,’us’,’jp’,’au’,’sz’};
    server2[]={‘uk’,’fr’,’us’};
    which server selected and their countries are selected then to add that server array.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: