Skip to content

Instantly share code, notes, and snippets.

@matthanger
Created December 6, 2011 23:09

Revisions

  1. matthanger created this gist Dec 6, 2011.
    18 changes: 18 additions & 0 deletions namespace.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    /**
    * Registers the specified namespace, ensuring all parts of the namespace are created. If the namespace
    * already exists, it will not be modified.
    *
    * Usage:
    * namespace('foo.bar');
    * foo.bar.baz = 'qux';
    *
    * @param {String} namespace The namespace to register. Namespaces are hierarchal, and are separated by dots.
    */
    window.namespace = function(namespace) {
    var parts = namespace.split(".");
    var root = window;

    for(var i=0; i<parts.length; i++) {
    root = root[parts[i]] = root[parts[i]] || {};
    }
    };