The Module Pattern
Establish a private context to run your code in and returns the result.
JavaScript:
-
(function() {
-
-
/* Code Here */
-
-
})();
Some features of this pattern.
Importing variables to act as local.
JavaScript:
-
(function(win, doc, accent){
-
// Same as Sfdc.log();
-
accent.log(win.location.href);
-
})(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.
JavaScript:
No comments
-
var myAppsAPI = (function() {
-
// These two functions cannot be called from outside this module.
-
// And you won't have name collisions incase someone else decides to
-
// create a drawTo() global function later.
-
function init() {};
-
function drawTo() {};
-
-
// then someone else could call your api in this fashion.
-
// myAppsAPI.draw(Sfdc.get("elementId"));
-
return {
-
draw: function(div){
-
init();
-
drawTo(div);
-
}
-
}
-
})();