Establish a private context to run your code in and returns the result.

  1. (function() {
  2.  
  3. /* Code Here */
  4.  
  5. })();

Some features of this pattern.

Importing variables to act as local.

  1. (function(win, doc, accent){
  2.     // Same as Sfdc.log();
  3.     accent.log(win.location.href);
  4. })(window, document, Sfdc);

This has the benefit of referencing each parameter as a local which improves performance.
Also the input can be changed while the implementation inside the module wouldn’t need to be touched.

Return a result.

  1. var myAppsAPI = (function() {
  2.     // These two functions cannot be called from outside this module.
  3.     // And you won't have name collisions incase someone else decides to
  4.     //   create a drawTo() global function later.
  5.     function init() {};
  6.     function drawTo() {};
  7.    
  8.     // then someone else could call your api in this fashion.
  9.     // myAppsAPI.draw(Sfdc.get("elementId"));
  10.     return {
  11.         draw: function(div){
  12.             init();
  13.             drawTo(div);
  14.         }
  15.     }
  16. })();
No comments