Thursday, July 3, 2008

Trick to use static variables in javascript

Maybe people that have background in C language know how to do this because in C language there is a static keyword to declare a static variable. What is about Javascript?

Well, Javascript doesn't have static keyword. One way to solve this problem, you may use global variable. However, for some people, it will pollute the global namespace. Last night, I read a javascript book, Javascript - The Definitive Guide, 5th edition. The author used closure to achieve this by returning back the anonymous function.


var uniqueID = (function() {
var id = 0; // This is the private persistent value
// The outer function returns a nested function that has access
// to the persistent value. It is this nested function we're storing
// in the variable uniqueID above.
return function() { return id++; }; // Return and increment
})(); // Invoke the outer function after defining it.



uniqueID(); // 0

uniqueID(); // 1

uniqueID(); // 2

19 comments:

_es said...

with({n:0})var id=function(){return n++;};

chamnap said...

great, thanks

George Garchagudashvili said...

good job really :)

Anonymous said...

Genial brief and this fill someone in on helped me alot in my college assignement. Say thank you you on your information.

Anonymous said...

i very love your writing choice, very unique.
don't give up and also keep creating in all honesty , because it just simply nicely to follow it,
impatient to looked over additional of your own web content, have a good day!

Edu said...

This is good. The problem is if you put that static var inside a simulated object.

function test(){
//your code (suppose static_var)
}

var test1 = new test();
var test2 = new test();

Your code doest work here, because we have two different "objects".

Do you know a way to do this?

Thanks

Edu said...

If you want to do that (i've found out now):

is a lot of code for a static variable but:

function FormEl(options){
if(typeof FormEl.n_objects == 'undefined')
FormEl.n_objects=0;
FormEl.n_objects++;
var n_objects = FormEl.n_objects;

alert("One More");
alert(n_objects);
}

var o1 = new FormEl();
var o2 = new FormEl();

chamnap said...

Edu: Your solution is fine, but it adds a public property "n_objects" to FormEl object.

Anonymous said...

var Foo = (function() {

// number times instantiated
var instances = 0

return function() {

// unique instance number
var _instance = ++instances;

// get object instance number
this.getInstanceNum = function() {
return _instance;
};

};

})();

var foo1 = new Foo();
var foo2 = new Foo();
var foo3 = new Foo();

alert(foo1.getInstanceNum());
alert(foo2.getInstanceNum());
alert(foo3.getInstanceNum());

alert(foo1.getInstanceNum());
alert(foo2.getInstanceNum());
alert(foo3.getInstanceNum());

chamnap said...

Yes, that's fine too. You use singleton pattern in javascript.

Anonymous said...

http://www.lv-designer-handbags.net/ Louis Vuitton Speedy

Anonymous said...

buy tramadol online cod tramadol xanax high - buy ultram canada

Curvve said...

A good website design is very important for any business. It is advised to not design heavy sites .now a days website design growth is extreme level but how effective that website.

Secure Web Design

Anonymous said...

ambien internet order where can you buy ambien over the counter - purchase ambien online canada

Anonymous said...

purchase ambien overnight delivery where to buy ambien cr in canada - where to buy ambien in mexico

Anonymous said...

Super Awesome Technique..
Thanks a lot for sharing.

Cado Magenge said...

App Development Company should be able of doing a complete web development and web design activities. There are some companies which will offer only front end designing by using web designers and some of them will have only web developers who will code the back end with high quality but they will have very little knowledge in front end designing. So, always find a company where there will be both designers and developers which will be helpful in developing the complete website in at place.

Cado Magenge
" web development company melbourne | mobile apps development companies melbourne
website development company melbourne | magento development company "

Claudia D said...

Hi,

Very good solution! I am new to javascript and I want to send a parameter in order to start from another number dinamically.
For example I want to call uniqueId(3) and in the function the id should start from 3. How would the function would look like?

Bangalore Web Guru said...

The blog was absolutely fantastic! Lot of great information which can be helpful in some or the other way. Keep updating the blog, looking forward for more contents...Great job, keep it up..
Web Design Bangalore | Website Development Bangalore

Subscribe in a Reader