vendredi 25 mars 2016

Identifying best pattern for exposing JS utility functions

I'm trying to build a JS file for our project which will expose some generic utility functions. I've used the following two patterns before but not convinced with them entirely.

Pattern 1

var objUtils = {
        foo: function(){
                //code
        },
        bar: function(){
                //code
                //foo can't be called
        }
}

The problem with the above pattern is that I can't invoke cross-invoke the functions.

Pattern 2

var objUtils = (function(){
        var foo = function(){
        },
        bar = function(){
        }
        
        return{
                foo : foo,
                bar : bar
        }
})();

The problem with the above pattern is that every time I add a new function I've to make sure that I add it to the return block to make it exposed, which I feel is kind of a redundant work.

Any thoughts?

Aucun commentaire:

Enregistrer un commentaire