1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Kỹ thuật lập trình >

Wrapper Objects LiveConnect Data Conversion

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (3.5 MB, 461 trang )


When a JavaObject is used in a boolean context, it is converted to a boolean value by invoking the
T
booleanValue
T
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 string context, it is converted to a string value by invoking the
T
toString
T
method of the Java object it represents. All Java objects define or inherit this method, so this conversion always succeeds.
When a JavaObject is used in an object context, no conversion is necessary, since it is already a JavaScript object.
Because of these different conversion rules, and for other reasons as well, JavaObject objects behave differently than other JavaScript objects, and there are some common
pitfalls that you need to recognize. First, it is not uncommon to work with a JavaObject that represents an instance of
T
java.lang.Double
T
or some other numeric object. In many ways, such a JavaObject behaves like a primitive number value, but be careful when
using the
T
+
T
operator. When you use a JavaObject or any JavaScript object with
T
+
T
, you are specifying a string context, so the object is converted to a string for string
concatenation instead of being converted to a number for addition.
When you want to explicitly convert a JavaScript object to a primitive value, you usually call its
T
valueOf
T
method. Note that this does not work with JavaObject objects. As we discussed earlier, the JavaObject class defines no properties of its own; all of its
properties represent fields and methods of the Java object it represents. This means that JavaObject objects dont support common JavaScript methods, such as
T
valueOf
T
. In the case of our JavaObject-wrapped
T
java.lang.Double
T
object, you should call the Java
T
doubleValue
T
method when you need to force the object into a primitive value. Another difference between JavaObject objects and other JavaScript data types is that
JavaObjects can be used in a boolean context only if they define a
T
booleanValue
T
method. Suppose
T
button
T
is a JavaScript variable that may contain
T
null
T
or may hold a JavaObject that represents an instance of the
T
java.awt.Button
T
class. If you want to check whether the variable contains
T
null
T
, you might write code like this, out of habit:
if button { ... }
If
T
button
T
is
T
null
T
, this works fine. But if
T
button
T
actually contains a JavaObject representing a
T
java.awt.Button
T
instance, LiveConnect tries to invoke the
T
booleanValue
T
method. When it discovers that the
T
java.awt.Button
T
class doesnt define one, it causes a JavaScript error. The workaround in this case is to be explicit about what you are testing
for, to avoid using the JavaObject in a boolean context:
if button = null { ... }
This is a good habit to get into, in any case, since it makes your code easier to read and understand.

Xem Thêm
Tải bản đầy đủ (.pdf) (461 trang)

×