Code Snippet
Namespaced Javascript Template
Self invoking anonymous function assigned to the yournamespacechoice global variable. Serves the effect of keeping all functions and variables private to this function. To expose a function or variable we must explictly return it at the bottom of the function. Remaps jQuery to $.
var yournamespacechoice = (function ($) {
var publicfunction;
function privatefunction() {
// function only available within parent function
}
publicfunction = function publicfunction() {
// public function available outside of this funtion
};
// Expose any functions that we need to access outside of this scope. Use yournamespacechoice.functionName() to call them.
return {
publicfunction: publicfunction
};
}(window.$));
This type of function statement needs to exist before it is used.
Shouldn’t you pass though window.jQuery? incase noConflict has been called earlier?
Top marks Chris. I have using Glow as my JS library recently and have been struggling to write plugins without jQuery. Now when I map glow.dom.get to $ as the universal selector and use this namespace method I can make some truly powerful stuff!
Cheers!
DigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.