Skip to content

Instantly share code, notes, and snippets.

@arboleya
Forked from giuliandrimba/namespace.js
Last active December 10, 2015 17:08
Show Gist options
  • Save arboleya/4465612 to your computer and use it in GitHub Desktop.
Save arboleya/4465612 to your computer and use it in GitHub Desktop.
Simple namespace definer.
/*
USAGE:
# at window
ns( 'my.namespace' ).MyClass = ...
# at app obj
var app = {}
ns( 'my.namespace', app ).MyClass = ...
*/
function ns( ns, base )
{
var n, b = base || window, r = /[^\.]+/g;
while( (n = r.exec( ns ) ) != null )
b = b[n[0]] || (b[n[0]] = {});
return b;
}
@arboleya
Copy link
Author

arboleya commented Jan 6, 2013

Testing (default = at window)

ns( 'a.b.c.d.e'  );

console.log( window.a );
console.log( window.a.b );
console.log( window.a.b.c );
console.log( window.a.b.c.d );
console.log( window.a.b.c.d.e );

More (custom - inside app obj)

var app = {}
ns( 'a.b.c.d.e', app );

console.log( app.a );
console.log( app.a.b );
console.log( app.a.b.c );
console.log( app.a.b.c.d );
console.log( app.a.b.c.d.e );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment