Skip to content
simple javascript namespacing
JavaScript CoffeeScript
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
tests
.gitignore
Gruntfile.js
LICENSE
README.md
bower.json
ns.coffee
ns.js
ns.min.js
package.json

README.md

ns.js

simple javascript namespacing

examples

Create a namespace and its object:

var obj = ns("com.app.someVar", 254);
// obj = 254

get a namespace path:

var obj = ns("com.app.someVar") // already created
// obj === 254

var objError = ns("com.app.someOtherVar") // does not exists
// objError === undefined

obscure namepsace

if you wish NOT to attach a namespace to window and obscure it from being global you may pass as a first parameter 'true' or an exisitng object, your objects will get attached to a variable under window.___ by default or to the object passed -- if you know of a better way, fork and send pull request

passing true:

var obj = ns(true, "obscured.namespace", 1);
// window.obscured === undefined
// window.___.obscured === [obj]

passing an object:

// be sure the object you pass actually exists if not create it
window.mynamespace = window.mynamespace || {};

// ON GLOBAL NAMESPACE
var obj = ns(window.mynamespace, "custom.namespace", 1);
// window.custom === undefined
// window.mynamespace.custom === [obj]
// obj === 1

// OR LOCAL NAMESPACE
var local = {}; 
var obj = ns(local, "custom.namespace", 1);
// window.local === undefined
// local.custom === [obj]
// obj === 1
Something went wrong with that request. Please try again.