tailieunhanh - Test Driven JavaScript Development- P9

Test Driven JavaScript Development- P9:This book is about programming JavaScript for the real world, using the techniques and workflow suggested by Test-Driven Development. It is about gaining confidence in your code through test coverage, and gaining the ability to fearlessly refactor and organically evolve your code base. It is about writing modular and testable code. It is about writing JavaScript that works in a wide variety of environments and that doesn’t get in your user’s way. | Object Composition and Mixins 153 accept an optional properties argument. We will discuss this method further in Chapter 8 ECMAScript 5th Edition. The tddj 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 object 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 to consider arguments an object that walks like a duck swims like a duck and quacks like a duck and therefore is a duck or rather an array . This means that we can borrow methods from by calling them with arguments as this as seen in Listing . Listing Borrowing from test arguments should borrow from function function addToArray var args arguments var arr return 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 shows an example. Please purchase PDF Split-Merge on to remove this watermark. From the Library of 154 Objects and Prototypal Inheritance Listing Borrowing explicitly test arguments should borrow explicitly from .

crossorigin="anonymous">
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.