HTML and CSS Reference
In-Depth Information
Listing 7.51 Borrowing explicitly
"test arguments should borrow explicitly from Array.prototype":
function () {
function addToArray() {
arguments.slice = Array.prototype.slice;
var args = arguments.slice();
var arr = args.shift();
return arr.concat(args);
}
var result = addToArray([], 1, 2, 3);
assertEquals([1, 2, 3], result);
}
Using this technique, we can build objects that are collections of methods related
over some topic, and then add all the properties of this object onto another object
to “bless” it with the behavior. Listing 7.52 shows the initial test case for a method
that will help us do exactly that.
Listing 7.52 Initial test case for tddjs.extend
TestCase("ObjectExtendTest", {
setUp: function () {
this.dummy = {
setName: function (name) {
return (this.name = name);
},
getName: function () {
return this.name
||
null;
}
};
},
"test should copy properties": function () {
var object = {};
tddjs.extend(object, this.dummy);
assertEquals("function", typeof object.getName);
assertEquals("function", typeof object.setName);
}
});
 
Search WWH ::




Custom Search