HTML and CSS Reference
In-Depth Information
Tests pass. Now, is there any duplication to remove? That full namespace is al-
ready starting to stick out as slightly annoying. By wrapping the test in an anonymous
closure, we can “import” the ajax namespace into the local scope by assigning it
to a variable. It'll save us four keystrokes for each reference, so we go for it, as seen
in Listing 12.13.
Listing 12.13 “Importing” the ajax namespace in the test
(function () {
var ajax = tddjs.ajax;
TestCase("GetRequestTest", {
"test should define get method": function () {
assertFunction(ajax.get);
},
"test should throw error without url": function () {
assertException(function () {
ajax.get();
}, "TypeError");
}
});
}());
We can apply the same trick to the source file as well. While we're at it, we
can utilize the scope gained by the anonymous closure to use a named function as
well, as seen in Listing 12.14. The function declaration avoids troublesome Internet
Explorer behavior with named function expressions, as explained in Chapter 5,
Functions.
Listing 12.14 “Importing” the ajax namespace in the source
(function () {
var ajax = tddjs.namespace("ajax");
function get(url) {
if (typeof url != "string") {
throw new TypeError("URL should be string");
}
}
ajax.get = get;
}());
 
Search WWH ::




Custom Search