How to namespace Javascript

Jumpino 2.0 will have a much larger client (written in JS) that will facilitate many many features currently not available in 1.0. So clearly, I’ll need a way to organize application code. So after checking out some JS namespacing methods on StackOverflow, I made my own:

[js]
function namespace(n) {
var ns = [n];
return n;
}

function using_namespace() {
var _ns = {};
for (_a = 0; _a <= arguments.length; ++_a) {
for (__p in arguments[_a]) {
if (__p in _ns) {
if (__p != 0)
throw new Error(‘Ambiguous member .’ + __p +’ in ‘+ arguments[_a] + ‘.’);
continue;
}
_ns[__p] = arguments[_a][__p];
}
}
return _ns;
}
[/js]

And here’s an example that can be generalized:

[js]
var jpo = jpo | new namespace("Jumpino");
jpo.Foo = function() {
return 3;
};

var yourapp = yourapp | new namespace("SomeApp");
yourapp.version = {stuff:"rawr",v:3};

yourapp.Bar = function() {
return 5;
};

var stl = stl | new namespace("Standrad Libraries");

stl.Baz = function() {
return 6;
};

// Look ma’, multiple namespaces
with(using_namespace(jpo, yourapp, stl)) {
Foo();
alert(version.stuff);
}
[/js]

I like the way the syntax looks and I like how ambiguous members are caught. What I do not like is how scoped methods/members are defined. namespace.Func = function() {} is kind of lame.

I’m not sure if I’ll settle with this but it’s a pretty decent paradigm.