Java Reference
In-Depth Information
You can also specify some Boolean attributes to the properties of an Object. For
example, you can set the writable attribute of an Object to false to make the property
read-only. There are several ways to create objects in Nashorn:
Using an Object Literal
Using a Constructor Function
Object.create() Method
The following section will explain how to create objects using these methods.
Using
Using an Object Literal
An object literal is an expression that creates and initializes an object. The syntax to use
an object literal is:
{propName1:value1, propName2:value2, propName3:value3,...}
The object literal is enclosed in braces. Each property consists of a name-value
pair. The name and value of the property are separated by a colon. Two properties are
separated by a comma. A trailing comma after the last property value is allowed. In the
object literal, propName1 , propName2 , and so on are the names of the properties and
value1 , value2 , and so on are their values. The name of a property can be an identifier,
a string enclosed in single or double quotes, or simply a number. If the property name
contains whitespaces, it must be enclosed in single or double quotes. You can also use the
empty string as the property name of an object.
The properties that you define this way are called own properties of the object.
Notice that an object may inherit properties from its prototype and those properties are
called inherited properties. The following statement creates few objects using object
literals:
// An object with no own properties
var emptyObject = {};
// An object with two own properties named x and y
var origin2D = {x:0, y:0};
// An object with three own properties named x, y, and z
var origin3D = {x:0, y:0, z:0};
// An object with whitespaces in property names
var redColor = {"red value": 1.0,
green: 0.0,
"black value": 0.0,
alpha: 1.0};
 
Search WWH ::




Custom Search