Danh mục

Test Driven JavaScript Development- P9

Số trang: 20      Loại file: pdf      Dung lượng: 191.12 KB      Lượt xem: 20      Lượt tải: 0    
tailieu_vip

Hỗ trợ phí lưu trữ khi tải xuống: 6,000 VND Tải xuống file đầy đủ (20 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Test Driven JavaScript Development- P9:This book is about programming JavaScript for the real world, using the techniquesand workflow suggested by Test-Driven Development. It is about gaining confidencein your code through test coverage, and gaining the ability to fearlessly refactor andorganically evolve your code base. It is about writing modular and testable code. Itis about writing JavaScript that works in a wide variety of environments and thatdoesn’t get in your user’s way.
Nội dung trích xuất từ tài liệu:
Test Driven JavaScript Development- P9 7.5 Object Composition and Mixins 153 accept an optional properties argument. We will discuss this method further in Chapter 8, ECMAScript 5th Edition. 7.5.2 The tddjs.extend Method Often we want to borrow behavior from one or more other objects to build the functionality we’re after. We’ve seen this a couple of times already. Remember the arguments object? It acts roughly like an array, but it is not a true array, and as such, lacks certain properties we might be interested in. The arguments ob- ject does, however, possess the most important aspects of an array: the length property, and numerical indexes as property names. These two aspects are enough for most methods on Array.prototype to consider arguments an object that “walks like a duck, swims like a duck, and quacks like a duck” and there- fore is a duck (or rather, an array). This means that we can borrow methods from Array.prototype by calling them with arguments as this, as seen in Listing 7.50. Listing 7.50 Borrowing from Array.prototype test arguments should borrow from Array.prototype: function () { function addToArray() { var args = Array.prototype.slice.call(arguments); var arr = args.shift(); return arr.concat(args); } var result = addToArray([], 1, 2, 3); assertEquals([1, 2, 3], result); } The example borrows the slice function and calls it on the arguments object. Because we don’t give it any other arguments, it will return the whole array, but the trick is now we’ve effectively converted arguments to an array, on which we can call the usual array methods. Remember in Chapter 5, Functions, we illustrated implicit binding of this by copying a function from one object to another. Doing so causes both objects to share the same function object, so it’s a memory efficient way to share behavior. Listing 7.51 shows an example.Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com 154 Objects and Prototypal Inheritance 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); } });Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com 7.5 Object Composition and Mixins 155 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) { ...

Tài liệu được xem nhiều: