Utility class for functions

You need different syntax to work with an IIFE, but I believe it is good practise to do so

var Utils = (function(){

 return {
    test: function () { ... },
    test1: function () { ... }
    ...
 }

}())

For a reasonably-sized project usually you'll have a project namespace under which all the project code falls so you don't unnecessarily pollute the global namespace. You can pass in the namespace as an argument to the IIFE and attach the code you create within it to the namespace. Something like this:

;(function(namespace) {

  namespace.Utils = {
    test: function() {
      console.log('test');
    },
    test1: function() {
      console.log('test1');
    }
  };

}(this.namespace = this.namespace || {}));

namespace.Utils.test(); // test

Tags:

Javascript