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


  1. User Avatar
    Permalink to comment#

    This type of function statement needs to exist before it is used.

  2. User Avatar
    Permalink to comment#

    Shouldn’t you pass though window.jQuery? incase noConflict has been called earlier?

  3. User Avatar
    Andy Bell
    Permalink to comment#

    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!


Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.