Java Reference
In-Depth Information
Table 12-3. ( continued )
Method
Description
void removeMember(String
name)
Removes the specified property from the object.
void setMember(String name,
Object value)
Sets the specified value to the specified property
name of this object. If the property name does not
exist, a new property with the specified name is
added.
void setSlot(int index,
Object value)
Sets the specified value to the specified indexed
property of this object. If the index does not exist, a
new property with the specified index is added.
double toNumber()
Returns the object's numeric value. Typically,
you will use this method if the script object is
a wrapper for a numeric value such as a script
object created in Nashorn using the expression new
Object(234.90) . On other script objects, it returns
the value Double.NAN .
Collection<Object> values()
Returns all property values of this object in a
Collection .
I will show how to use these methods and some methods of the ScriptObjectMirror
class in the subsequent section. The JSObject interface can be implemented by any Java
class. Objects of such a class can be used just as if they are script objects and can use the
syntax obj.func() , obj[prop] , delete obj.prop , and so on to work with their methods
and properties in scripts.
Using Properties of Script Objects
The ScriptObjectMirror class implements JSObject and Bindings interfaces. You can
use methods of both interfaces to access properties. You can use the getMember() and
getSlot() methods of JSObject to read the named and indexed properties of the script
object. You can use the get() method of Bindings to get the value of a property. You can
use the setMember() and setSlot() methods of JSObject to add and update a property.
The put() method of Bindings lets you do the same.
You can use the hasMember() and hasSlot() methods of JSObject to check
whether a named property and an indexed property exist. At the same time, you can
use the containsKey() method of the Bindings interface to check whether a property
exists. You can think of the JSObject and Bindings interfaces implemented by the
ScriptObjectMirror class providing two views of the same script object—the former as a
simple object and the latter as a map.
 
 
Search WWH ::




Custom Search