Java Reference
In-Depth Information
Defining Functions
In Nashorn, a function is a block of executable, parameterized code that is declared once
and can be executed multiple times. Executing a function is called calling or invoking the
function. A function is an object; it can have properties, and it can be passed around as
an argument to another function; it can be assigned to a variable. A function can have the
following parts:
A name
Formal parameters
A body
A function may optionally be given a name. When a function is called, you can pass
some values that are called arguments of that function. The arguments are copied to the
function's formal parameters. The body of the function consists of a series of statements.
A function may optionally return a value using a return statement. If no return
statement is used to return from the function, by default, the value undefined is returned.
A function in Nashorn can be defined in many ways, as described in subsequent sections.
You may think of a function in nashorn as a method in Java. however, be aware
that a function in nashorn is an object and it is used in many different ways that a method in
Java cannot be used.
Tip
Function Declaration
You can declare a function using the function statement as follows:
function functionName(param1, param2...) {
function-body
}
The keyword function is used to declare a function. The functionName is the name
of the function that can be any identifier. The param1 , param2 , and so on are formal
parameter names. A function may have zero formal parameters and, in that case, the
function name is followed with an opening and a closing parentheses. The body of the
function that consists of zero or more statements is enclosed in braces.
in strict mode, you cannot use eval and arguments as the function name or the
name of formal parameters of the function.
Tip
 
 
Search WWH ::




Custom Search