Namespacing in Javascript

After reading this article by , i was pretty much sold on the design pattern #5 suggested for namespacing your functions. Then I ran into some problems/questions. First off, I’d assume you want to do some amount of sub-levels into the namespace. I know the article says dont do it but sometimes i have an application lets say btp. Then i want to add something like btp.devices.functionToCall. I want to define the functionToCall method in a file called devices.js. I think the best way I have found to make this work so far is to do something like the following:

var btp = {}
function() {
  this.someOtherFunction = function() {
     // do something else
  }
}.apply(btp);

btp.devices = {}

function() {
   this.functionToCall = function() {
      // do stuff
   }
}.apply(btp.devices);

I think this seems clean enough but I dont know if there is a better way to do this or if it makes more sense to just start creating namespaces at the toplevel like btpDevices or something. Just thought I’d share where I ended up for now. Any ideas for a better way?

Be Sociable, Share!
This entry was posted in tech, Uncategorized and tagged , , . Bookmark the permalink.

4 Responses to Namespacing in Javascript

  1. Angus Croll says:
    Hi – I’m the author of the article :-)

    Yes for sure both dynamic namespace examples (#4 and #5) get tricky with nested namespaces. If you *really* want nested namespaces I think it makes sense to define the names statically

    Nice site btw!

    • Kiran says:
      Since you have suggested against doing nested namespaces what do you do when you have modules of functionality like i talked about. Just hope there is not a name conflict and make everything hang off the top level? Is there a better approach that i should be using and thanks for the site comment. I have been following your site for a while and its a really good JS details site :)
  2. Rik Sagar says:
    Hey Kiran,

    Why even split it into two operations, you could just do the following:

    var btp = {}
    function() {
    this.someOtherFunction = function() {
    // do something else
    }

    this.devices = {
    functionToCall: function(){
    // Do stuff
    }
    }
    }.apply(btp);

    I’m assuming the “devices” part is an expected part of the objects interface.

    Generally, I like the “Module Pattern” described in Angus’ article and http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth

    It allows you to hide away your private variables, internal state, callbacks, etc., very cleanly. I very rarely use the “this” keyword anymore.

    When I see “o.addEventListener(‘foo’, this._foocb.bind(this))” I cringe.

  3. J. Weir says:
    If you are taking a functional approach I recommend giving http://jweir.github.com/namespace/ a try. It is a very simple implementation for namespacing inspired by Erlang’s modules.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>