HTML and CSS Reference
In-Depth Information
The Array.prototype.indexOf method returns a number less than 0 if
the element is not present in the array, so checking that it returns a number equal
to or greater than 0 will tell us if the observer exists.
11.3.1.2 Solving Browser Incompatibilities
Running the test produces somewhat surprising results as seen in the relevant excerpt
in Listing 11.19.
Listing 11.19 Funky results in Internet Explorer 6
chris@laptop:~/projects/observable $ jstestdriver --tests all
.EE
Total 3 tests (Passed: 1; Fails: 0; Errors: 2) (11.00 ms)
Microsoft Internet Explorer 6.0 Windows: Run 3 tests \
(Passed: 1; Fails: 0; Errors 2) (11.00 ms)
Observable.hasObserver.test \
should return true when has observer error (11.00 ms): \
Object doesn't support this property or method
Observable.hasObserver.test \
should return false when no observers error (0.00 ms): \
Object doesn't support this property or method
Internet Explorer versions 6 and 7 failed the test with their most generic of error
messages: “Object doesn't support this property or method.” This can indicate any
number of issues.
We are calling a method on an object that is null .
We are calling a method that does not exist.
We are accessing a property that doesn't exist.
Luckily, TDD-ing in tiny steps, we know that the error has to relate to the re-
cently added call to indexOf on our observers array. As it turns out, IE 6 and 7 does
not support the JavaScript 1.6 method Array.prototype.indexOf (which we
cannot really blame it for, it was only recently standardized with ECMAScript 5,
December 2009). In other words, we are dealing with our first browser compatibility
issue. At this point, we have three options:
Circumvent the use of Array.prototype.indexOf in hasObserver ,
effectively duplicating native functionality in supporting browsers
Implement Array.prototype.indexOf for non-supporting browsers.
Alternatively implement a helper function that provides the same functionality
 
Search WWH ::




Custom Search