Java Reference
In-Depth Information
Functions can be defi ned in a variety of ways. You can create a function using the following standard
function statement:
function functionName() {
// code here
}
You can also create an anonymous function and assign it to a variable. The following code demonstrates
this approach:
var functionName = function() {
// code here
};
The trailing semi-colon is not a typo because this statement is an assignment operation, and all assign-
ment operations should end with a semi-colon.
Functions are objects, and thus they have a constructor. It's possible to create a function using the
Function object's constructor as shown in the following code:
var functionName = new Function(“arg1”, “arg2”, “return arg1 + arg2”);
The fi rst arguments to the constructor are the names of the function's parameters — you can add as
many parameters as you need. The last parameter you pass to the constructor is the function's body.
The previous code creates a function that accepts two arguments and returns their sum.
There are very few instances where you will use the Function constructor. It is preferred to defi ne a
function using the standard function statement or by creating an anonymous function and assigning
it to a variable.
Properties
Property
Introduced
Description
arguments
JavaScript 1.1
An array containing the parameters passed into the
function.
arguments.length
JavaScript 1.1
Returns the number of parameters passed into the
function.
constructor
JavaScript 1.1
Used to reference the constructor function for the object.
length
JavaScript 1.1
Returns the number of parameters expected by the
function. This differs from arguments.length , which
returns the number of parameters actually passed into
the function.
prototype
JavaScript 1.1
Returns the prototype for the object, which can be used
to extend the object's interface.
Search WWH ::




Custom Search