Set my var to an anonymous function with parameter?

I am creating my first OO JS library, and I have a little problem with one part, which is probably very simple ...

I have it:

var storageLocker = function(catalog){
    if(catalog){
        this.catalog = catalog;
    }
    //my code...
}()

I need to do what other libraries do, such as jQuery, where you can select an element (in my case, select the localStorage element) and then connect other functions to it. I had everything that works, but for best practice and to make it more extensible later, I put it in an anonymous function, and now I can’t figure out how to have the syntax:

storageLocker('localStorageItem').save({"item":"an example item saved to localStorageItem"})

but right now, if I do this with this syntax, it returns this error:

Uncaught TypeError: Property 'storageLocker' of object [object DOMWindow] is not a function

Any ideas?

0
source share
1 answer

() .

var storageLocker = function(...) { ... }(), , storageLocker.

function anonymous(...) { ... };
var storageLocker = anonymous();

, storageLocker undefined .

+4

Source: https://habr.com/ru/post/1777359/


All Articles