JavaScript language advanced Tips & Tricks

http://code.google.com/p/jslibs/wiki/JavascriptTips#Singleton_pattern

Java script singleton: http://www.hardcode.nl/subcategory_1/article_526-singleton-examples-in-javascript.htm

var Setting =(function(){
    var instantiated;
    function init (){
        // all singleton code goes here
        return {
            publicWhatever:function(){
                alert('whatever')
            },
            userID:null,
          
            setUserID:function( id ) {
                userID = id;
            },
          
            getUserID:function() {
                return userID;
            }
        }
    }

    return {
        getInstance :function(){
            if (!instantiated){
                instantiated = init();
            }
            return instantiated;
        }
    }
})()

Comments

Popular posts from this blog

董事長您好

After reading Steve Jobs Autobiography

Drawing textured cube with Vulkan on Android