JavaScript Conversion of JavaObjects
Having worked your way through the previous dense section, you may hope that we are done with the topic of data conversion. Unfortunately, there is more to be discussed on the topic of how JavaScript converts JavaObject objects to various JavaScript primitive types. Notice in Figure 22-3 that quite a few Java data types, including Java strings (instances of java.lang.String), are converted to JavaObject objects in JavaScript rather than being converted to actual JavaScript primitive types, such as strings. This means that when you use LiveConnect, you’ll often be working with JavaObject objects.
Refer back to Table 11-1, which shows how various
JavaScript data types are converted when used in various contexts.
For example, when a number is used in a string context, it is
converted to a string, and when an object is used in a boolean
context, it is converted to the value false
if it
is null
and true
otherwise.
These conversion rules don’t apply to JavaObject objects, which
are converted using their own rules, as follows:
When a JavaObject is used in a numeric context, it is converted to a number by invoking the
doubleValue( )
method of the Java object it represents. If the Java object does not define this method, a JavaScript error occurs.When a JavaObject is used in a boolean context, it is converted to a boolean value by invoking the
booleanValue( )
method of the Java object it represents. If the Java object does not define this method, ...
Get JavaScript: The Definitive Guide, Fourth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.