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
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) { ...
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ìm kiếm theo từ khóa liên quan:
thiết kế web giáo trình css kĩ thuật cắt html flash cơ bản giáo trình photoshopTài liệu liên quan:
-
Báo cáo thực tập: Đề tài thiết kế Web
77 trang 569 2 0 -
Đề thi thực hành môn Thiết kế Web - Trường Cao đẳng nghề Vĩnh Phúc
3 trang 269 2 0 -
182 trang 177 0 0
-
MỘT SỐ ĐIỂM CẦN CHÚ Ý KHI THIẾT KẾ WEB
5 trang 113 0 0 -
GIÁO TRÌNH LẬP TRÌNH WEB_PHẦN 2_BÀI 3
3 trang 103 0 0 -
Giáo trình Nhập môn thiết kế website
58 trang 83 0 0 -
Tài liệu giảng dạy Thiết kế giao diện Web - Trường CĐ Kinh tế - Kỹ thuật Vinatex TP. HCM
88 trang 72 0 0 -
81 trang 69 0 0
-
112 trang 64 0 0
-
Hướng dân sử dụng Navicat để Create , Backup , Restore Database
7 trang 63 0 0