tailieunhanh - Phát triển Javascript - part 28

Tham khảo tài liệu 'phát triển javascript - part 28', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Observing Arbitrary Events 243 other two functions and their tests as an exercise. When you are done you will note that one of the tests keep failing. We will deal with that last test together. Supporting Events in notify While updating notify to accept an event whose observers to notify one of the existing tests stays in the red. The test in question is the one that compares arguments sent to notify against those received by the observer. The problem is that because notify simply passes along the arguments it receives the observer is now receiving the event name in addition to the arguments it was supposed to receive. To pass the test Listing uses to pass along all but the first argument. Listing Passing all but the first argument to observers function notify event . var args arguments 1 for var i 0 l i l i try i .apply this args catch e This passes the test and now observable has the interface to support events even if it doesn t actually support them yet. The test in Listing specifies how the events are supposed to work. It registers two observers to two different events. It then calls notify for only one of the events and expects only the related observer to be called. Listing Expecting only relevant observers to be called test should notify relevant observers only function var calls event function event other function Download from 244 The Observer Pattern other other assertEquals other calls The test obviously fails as the observable happily notifies all the observers. There is no trivial way to fix this so we roll up our sleeves and replace the observable array with an object. The new object should store observers in arrays on properties whose keys are event names. Rather than conditionally initializing the object and array in all

TỪ KHÓA LIÊN QUAN
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.