Progo
Progo

Reputation: 3490

Tiny JavaScript library stopped working

For a previous question I answered here on SO, I made the following library.

(function(){
    var LS = function(){
        return new LS.fn.init();
    };
    LS.fn = LS.prototype ={
        //Check to see if the browser suports LocalStorage
        init : function(){
            this.ls = (typeof(Storage)!=="undefined") ? false : true;
            return this;
        }
    }
    LS.fn.init.prototype = LS.fn;
    LS.fn.init.prototype = {
        set : function(name, val){
            if(this.ls) localStorage.setItem(name, val);
        },
        get : function (name){
            if(this.ls) return localStorage.getItem(name);
        },
        remove : function(name){
            if(this.ls) localStorage.removeItem(name);
        },
        test: function(){
            alert("hi")
        }
    }
    window.LS = window.ls = LS;
})();

This was working fine until recently.

I am getting errors like:

LS.set("foo", "bar"); // Error: undefined is not a function... 

Even the following code gives an error:

LS.test();

Why has this stopped working?

Thank you.

NOTE: Previously, I had a typo, but now I have fixed that and it still doesn't work.

Upvotes: 0

Views: 47

Answers (1)

Tibos
Tibos

Reputation: 27823

Your window.LS is initialized to a function, and you seem to use it as if you expect it to be initialized to the return value of that function.

Change this line:

window.LS = window.ls = LS;

to:

window.LS = window.ls = LS();

Also, try to simplify your library code. It seems very convoluted. At the very least comment it out a bit to indicate what the various parts do/are used for. The part with the typo (LS.protorype) isn't used anywhere for example.

Upvotes: 2

Related Questions