Java Reference
In-Depth Information
printf("adder(10, 15) = %d", adder(10, 15));
printf("adder('Hello', ' world') = %s", adder('Hello', ' world'));
adder(10, 15) = 25
adder('Hello', ' world') = Hello world
Sometimes, you may need an empty function that takes no parameters and performs
no logic. An empty function always return undefined when called. You can create such an
empty function by not specifying any parameters to the Function , as shown:
// Define an empty function
var emptyFunction = Function();
// Print the string form of the new function
print(emptyFunction);
// Call the function that will return undefined
var nothing = emptyFunction();
print(nothing);
function () {
}
undefined
You are advised not to use the Function object to define functions because the
runtime cannot apply optimization to the function body contained in the string and the
function is created every time the expression using Function is encountered.
The Object Type
An Object in Nashorn is a collection of properties. There are two types of properties:
Named Data Property
Named Accessor Property
A named data property associates a name with a value. The value can be a primitive
value, an object, or a function. You can think of a named data property of an Object in
Nashorn as an instance variable or a method of a Java object.
A named accessor property associates a name with one or two accessor functions.
Those functions are also known as getter and setter. Accessor function are used to get
or set a value. When the named accessor property is used (assigned a value or read) the
corresponding accessor function is called. You can think of a named accessor property as
getter/setter methods of a Java object.
 
Search WWH ::




Custom Search