Đang chuẩn bị liên kết để tải về tài liệu:
Test Driven JavaScript Development- P7

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

Test Driven JavaScript Development- P7: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. | 6.4 Memoization 113 Listing 6.26 Memoizing the Fibonacci sequence in a closure var fibonacci function var cache function fibonacci x if x 2 return 1 if cache x cache x fibonacci x - 1 fibonacci x - 2 return cache x return fibonacci This alternative version of fibonacci runs many orders of magnitude faster than the original one and by extension is capable of calculating more numbers in the sequence. However mixing computation with caching logic is a bit ugly. Again we will add a function to Function.prototype to help separate concerns. The memoize method in Listing 6.27 is capable of wrapping a method adding memoization without cluttering the calculation logic. Listing 6.27 A general purpose memoize method if Function.prototype.memoize Function.prototype.memoize function var cache var func this return function x if x in cache cache x func.call this x return cache x Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. From the Library of WoweBook.Com 114 Applied Functions and Closures This method offers a clean way to memoize functions as seen in Listing 6.28. Listing 6.28 Memoizing the Fibonacci function TestCase FibonacciTest test calculate high fib value with memoization function var fibonacciFast fibonacci.memoize assertEquals 1346269 fibonacciFast 30 The memoize method offers a clean solution but unfortunately only deals with functions that take a single argument. Limiting its use further is the fact that it blindly coerces all arguments to strings by nature of property assignment which will be discussed in detail in Chapter 7 Objects and Prototypal Inheritance. To improve the memoizer we would need to serialize all arguments to use as keys. One way to do this which is only slightly more complex than what we already have is to simply join the arguments as Listing 6.29 does. Listing 6.29 A slightly better memoize method if Function.prototype.memoize Function.prototype.memoize function var cache var func this var join Array.prototype.join .

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã 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.