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?

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!
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.