HTML and CSS Reference
In-Depth Information
The test sets up a dummy object in the
setUp
method. It then asserts that
when extending an object, all the properties from the source object is copied over.
This method is definitely eligible for the Internet Explorer DontEnum bug, so
Listing 7.53 uses the
tddjs.each
method to loop the properties.
Listing 7.53
Initial implementation of
tddjs.extend
tddjs.extend = (function () {
function extend(target, source) {
tddjs.each(source, function (prop, val) {
target[prop] = val;
});
}
return extend;
}());
The next step, seen in Listing 7.54, is to ensure that the two arguments are safe
to use. Any object will do on both sides; we simply need to make sure they're not
null
or
undefined
.
Listing 7.54
Extending
null
"test should return new object when source is null":
function () {
var object = tddjs.extend(null, this.dummy);
assertEquals("function", typeof object.getName);
assertEquals("function", typeof object.setName);
}
Note the expected return value. Listing 7.55 shows the implementation.
Listing 7.55
Allowing target to be
null
function extend(target, source) {
target = target
||
{};
tddjs.each(source, function (prop, val) {
target[prop] = val;
});
return target;
}