Interesting idea. Can you explain the use case to me? Why would I want to use _.namespace() instead of something like:[the left column]

There is no difference in the outcome between the below. But there is a difference in style. I find the right to be a bit easier on the eyes.


  var obj = (function() {
    var ret = {};
    var private = function() { ... };
    ret.public = function() { private(); };
    return ret;
  })();

  (function(){
    namespace("obj", public);

    function private(){ ... };
    function public(){ private(); };
  })();

And it is very easy to take a function public. Here is the above example, with private taken public. Notice how little on the right had to change.


  var obj = (function() {
    var ret = {};
    ret.private = function() { ... };
    ret.public = function() { ret.private(); };
    return ret;
  })();

  (function(){
    namespace("obj", public, private);

    function private(){ ... };
    function public(){ private(); };
  })();

And when within the closure, or temporary scope, I don't have to think about whether to prepend `ret` onto a function, unless it from another namespace. I just focus on the functions, not on if they are public or not.


  var another = (function(){
    var ret = {};

    ret.public = function(){
      return obj.public();
    }
    return ret;
  })();
  

  (function(){
    namespace("another", public);

    function public(){
      return obj.public();
    }
  })();

Lets look at a more complex example. When working in the scope I only think about the function, not book_ns + function.


  var book = (function(){
    book_ns = {}; // **

    book_ns.get_id = function(event){
      // return ...
    }

    book_ns.edit = function(event){
      var book = book_ns.get_id(event);
      // ...
    }

    book_ns.move = function(event){
      var book = book_ns.get_id(event);
      // ...
    }

    book_ns.destroy = function(event){
      var book = book_ns.get_id(event);
      // ...
    }
    return book;
  })();

  (function(){
    namespace("book", get_id, edit, move, destroy);

    function get_id(event){
      // return ...
    }

    function edit(event){
      var book = get_id(event);
      // ...
    }

    function move(event){
      var book = get_id(event);
      // ...
    }

    function destroy(event){
      var book = get_id(event);
      // ...
    }

  })();

** Notice on the left I use book_ns. If I used book there would be a namespace collision with my local var book. Which maybe the most natural name.

You can create nested namespaces, even if the parent does not already exist. In the below, foo need not already exist.


  var foo = {};
  foo.bar = (function() {
    var ret = {};
    ret.public = function() {};
    return ret;
  })();

  (function(){
    namespace("foo.bar", public);
    function public(){};
  })();

So there you have it. I recommend giving it a try, that would be the real test on if it is worthy or not. You may like or you may not.

What I like about it is I think in functions, not in objects. Working this way gets me to think about the code in a different manner. This of course is style or fashion, and not right nor wrong. Just perhaps ugly or beautiful, which is in the eye of the beholder.

Finally, in the above examples I was using namespace, not _.namespace since that is how I have been working for a while and I like it as root function.