HTML and CSS Reference
In-Depth Information
anchorLightbox: function (anchor, options) {
/* ... */
}
};
In larger libraries, we might want better organization than simply defining
everything inside the same object. For example, the lightbox might live in tddjs.
ui , whereas ajax functionality could live in tddjs.ajax . Many libraries provide
some kind of namespace function to help with this kind of organizing. Organizing
all code inside a single file is not a sound strategy, and when splitting code inside the
same object across several files, knowing if the namespace object is already created
becomes an issue.
6.2.2.1 Implementing Namespaces
For this topic we will use the tddjs object to namespace reusable code that is
shared between chapters. To help with namespacing we will implement our own
function that will loop each level in the namespace—provided as a string—creating
objects that don't exist. Listing 6.15 shows a few test cases demonstrating its use
and side-effects.
Listing 6.15 Demonstrating the namespace function
TestCase("NamespaceTest", {
tearDown: function () {
delete tddjs.nstest;
},
"test should create non-existent object":
function () {
tddjs.namespace("nstest");
assertObject(tddjs.nstest);
},
"test should not overwrite existing objects":
function () {
tddjs.nstest = { nested: {} };
var result = tddjs.namespace("nstest.nested");
assertSame(tddjs.nstest.nested, result);
},
"test only create missing parts":
 
Search WWH ::




Custom Search