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

29 comments:

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

    ReplyDelete
  2. good job really :)

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

    ReplyDelete
  4. 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!

    ReplyDelete
  5. 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

    ReplyDelete
  6. 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();

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

    ReplyDelete
  8. 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());

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

    ReplyDelete
  10. 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

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

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

    ReplyDelete
  13. Super Awesome Technique..
    Thanks a lot for sharing.

    ReplyDelete
  14. 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 "

    ReplyDelete
  15. 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?

    ReplyDelete
  16. Thanks for the post.It helped me a lot.Thanks!!!!!!Keep the good work up!
    SEO Experts India
    SEO Experts India
    SEO Experts in Bangalore

    ReplyDelete
  17. I am really enjoying the theme/design of your web site. Do you ever run into any internet browser compatibility problems? A handful of my blog visitors have complained about my blog not operating correctly in Explorer but looks great in Safari. Do you have any ideas to help fix this issue?

    ReplyDelete
  18. Blog is too interesting and suggested also the web development and Social media marketing services Delhi in full descriptive way.

    ReplyDelete
  19. replica bags from turkey replica gucci bags e1f79o6t91 replica bags wholesale in divisoria my latest blog post o7c47y7x68 Ysl replica bags replica bags turkey replica bags in bangkok replica hermes bags y4q98j9x83 replica bags online pakistan

    ReplyDelete
  20. This comment has been removed by the author.

    ReplyDelete