Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Tham khảo tài liệu 'phát triển javascript - part 26', 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ả | 11.2 AddingObservers 223 tddjs.util is undefined @http localhost 4224 . observable_test.js 5 11.2.1.2 Making the Test Pass Fear not Failure is actually a good thing It tells us where to focus our efforts. The first serious problem is that tddjs.util doesn t exist. Listing 11.5 adds the object using the tddjs.namespace method. Save the listing in src observable.js. Listing 11.5 Creating the util namespace tddjs.namespace util Running the tests again yields a new error as seen in Listing 11.6. Listing 11.6 Tests still failing chris@laptop projects observable jstestdriver --tests all E Total 1 tests Passed 0 Fails 0 Errors 1 1.00 ms Firefox 3.6.3 Linux Run 1 tests Passed 0 Fails 0 Errors 1 1.00 ms Observable.addObserver.test should store function error 1.00 ms tddjs.util.Observable is not a constructor @http localhost 4224 . observable_test.js 5 Listing 11.7 fixes this new issue by adding an empty Observable constructor. Listing 11.7 Adding the constructor function function Observable tddjs.util.Observable Observable To work around the issues with named function expressions discussed in Chapter 5 Functions the constructor is defined using a function declaration inside an immediately called closure. Running the test once again brings us directly to the next problem seen in Listing 11.8. Download from www.eBookTM.com 224 The Observer Pattern Listing 11.8 Missing addObserver method chris@laptop projects observable jstestdriver --tests all E Total 1 tests Passed 0 Fails 0 Errors 1 0.00 ms Firefox 3.6.3 Linux Run 1 tests Passed 0 Fails 0 Errors 1 0.00 ms Observable.addObserver.test should store function error 0.00 ms observable.addObserver is not a function @http localhost 4224 . observable_test.js 8 Listing 11.9 adds the missing method. Listing 11.9 Adding the addObserver method function addObserver Observable.prototype.addObserver addObserver With the method in place Listing 11.10 shows that the test now fails in place of a missing observers array. Listing 11.10 The .