Introduction
Namespaces are a key component in modern JavaScript programming. Unfortunately, there's no clean implementation. Developers are required to manage nested objects across their applications which, in itself is a complex task.
Panda JS' namespace utility is inspired by YUI's. If you're already familiar with their implementation, you're familiar with Panda's.
Creating Namespaces
To create a namespace, simply invoke the namespace utility like so:
panda.namespace('com.example.myProject');This will create and return the com.example.myProject object for you to then work with. Of course, you could always write the plain JavaScript yourself...
var com = {
example : {
myProject : {}
}
};But who wants to write that mess at the top of their scripts anyway? Furthermore, the code above is destructive. If you had previously defined the com or com.example namespaces in other scripts, you just clobbered them -- thus defeating the purpose of your namespaces.
The namespace utility, on the other hand, is nondestructive. If any object along the way to myProject already exists, it will not be redefined. It's original value will be used instead. This is incredibly useful because if you wanted to do this safely your JavaScript code would need to look like this:
var com = window.com || {};
com.example = com.example || {};
com.example.myProject = com.example.myProject || {};