Java Reference
In-Depth Information
// Try adding a new property to point
point.z = 1; // Will throw an error in strict mode
// Check if the property z was added to point
print("point has property z = " + ("z" in point));
// point inherits from Object.prototype. Let us add a property
// named z in Object.prototype
Object.prototype.z = 1;
// Check if the property z was added to point. Now point inherits
// the proeprty named z from Object.prototype.
print("point has property z = " + ("z" in point));
// The following statement has no effect as the point object
// is still non-extensible.
point.z = 100;
print("z = " + point.z); // Reads the Object.prototype.z
isExtensible() = true
isExtensible() = false
point has property z = false
point has property z = true
z = 1
Accessing Missing Properties
Nashorn provides an extension to allow you to add a method to an object that is called
when a nonexistent property of an object is accessed. You can also provide such a
method for nonexistent methods. The property names are __noSuchProperty__ and
__noSuchMethod__ . Notice the two underscores before and after the property names.
Both properties are functions. If you are working on a project and need this behavior
everywhere, you should add these hooks to the Object.prototype , so they are available
in all objects by default. Listing 4-18 shows how to add these properties to the Object.
prototype . The function for the __noSuchProperty__ is passed the property name being
accessed. The function for the __noSuchMethod__ is passed the method name and all
arguments used in the method call. You can access all arguments using the arguments
object inside the function.
 
Search WWH ::




Custom Search