HTML and CSS Reference
In-Depth Information
return, window.event);
} else {
// Possibly fall back to DOM0 event properties or abort
This example employs more specific feature tests, and should ideally produce
fewer false positives. Unfortunately, it does not work at all in certain browsers. To
understand why, we need to familiarize ourselves with native and host objects.
10.2.3 Native and Host Objects
Any object whose semantics are described by the ECMAScript specification is
known as a native object . By the nature of their definition, the behavior of native
objects is generally predictable and, as such, using specific feature tests such as the
type-check in Listing 10.4 will usually provide valuable information. However, given
a buggy environment, we may encounter a browser whose typeof implementation
is doing the wrong thing even if the object in question is in fact callable and works
as expected. By making a feature test more specific we reduce the chances of false
positives, but at the same time we demand more from the environment, possibly
increasing the chances of false negatives.
Objects provided by the environment but not described by the ECMAScript
specification are known as host objects . For example, a browser's DOM implemen-
tation consists solely of host objects. Host objects are problematic to feature test
because the ECMAScript specification defines them very loosely; “implementation-
defined” is commonly found in the description of host object behavior.
Host objects are, among other things, afforded the luxury of defining their own
result for typeof . In fact, the third edition of the ECMAScript specification does
not restrict this result in any way, and host objects may return “undefined” when
used with typeof , should they so wish. Although attachEvent most definitely
is callable in Internet Explorer, the browser is not cooperative in purveying this
information when asked with typeof , as Listing 10.5 shows.
Listing 10.5 typeof and host objects in Internet Explorer
// true in Internet Explorer, including version 8
assertEquals("object", typeof document.attachEvent);
As if this result wasn't bad enough, other host objects such as ActiveX objects
are even worse to work with. Listing 10.6 shows a few surprising results.
Search WWH ::

Custom Search