HTML and CSS Reference
In-Depth Information
"test should return consistent id for object":
function () {
var object = {};
var id = tddjs.uid(object);
assertSame(id, tddjs.uid(object));
"test should return unique id":
function () {
var object = {};
var object2 = {};
var id = tddjs.uid(object);
assertNotEquals(id, tddjs.uid(object2));
"test should return consistent id for function":
function () {
var func = function () {};
var id = tddjs.uid(func);
assertSame(id, tddjs.uid(func));
"test should return undefined for primitive":
function () {
var str = "my string";
The tests can be run with JsTestDriver, as described in Chapter 3, Tools of
the Trade. This is not an exhaustive test suite, but it shows the basic behavior the
method will support. Note that passing primitives to the function will not work as
assigning properties to primitives does not actually add properties to the primitive—
the primitive is wrapped in an object for the property access, which is immediately
thrown away, i.e., new String("my string"). __ uid=3 .
The implementation is the interesting part. The uid method generates ids by
looking up a counter that is incremented every time an id is requested. We could store
this id as a property of the uid function object, but that would make it susceptible
Search WWH ::

Custom Search