Function
Specifies a string of MetaScript code to be compiled as a function.
Core object
Created by
The Function constructor:
new Function( [arg1[, arg2[, ... argN]],] functionBody )
function name( [param[, param[, ... param]]])
{
statements
}
Parameters
Parameter | Description |
---|---|
arg1, arg2, ... arg_N_ | Names to be used by the function as formal argument names. Each must be a string that corresponds to a valid MetaScript identifier; for example "x" or "theValue". |
functionBody | A string containing the MetaScript statements comprising the function definition. |
name | The function name. |
param | The name of an argument to be passed to the function. A function can have up to 255 arguments. |
statements | The statements comprising the body of the function. |
Description
Function objects created with the Function constructor are evaluated each time they are used. This is less efficient than declaring a function and calling it within your code, because declared functions are compiled.
To return a value, the function must have a return statement that specifies the value to return.
All parameters are passed to functions by value; the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function. However, if you pass an object as a parameter to a function and the function changes the object's properties, that change is visible outside the function, as shown in the following example:
The this keyword does not refer to the currently executing function, so you must refer to Function objects by name, even within the function body.
Accessing a function's arguments with the arguments array
You can refer to a function's arguments within the function by using the arguments array. See Function.arguments.
Specifying arguments with the Function constructor
The following code creates a Function object that takes two arguments.
The arguments "x" and "y" are formal argument names that are used in the function body, "return x * y".
The preceding code assigns a function to the variable multiply. To call the Function object, you can specify the variable name as if it were a function, as shown in the following examples.
Assigning a function to a variable with the Function constructor
Suppose you create the variable multiply using the Function constructor, as shown in the preceding section:
This is similar to declaring the following function:
Assigning a function to a variable using the Function constructor is similar to declaring a function with the function statement, but they have differences:
When you assign a function to a variable using var multiply = new Function("..."), multiply is a variable for which the current value is a reference to the function created with new Function().
When you create a function using function multiply(), {...} multiply is not a variable, it is the name of a function.
Nesting functions
You can nest a function within a function. The nested (inner) function is private to its containing (outer) function:
The inner function can be accessed only from statements in the outer function.
The inner function can use the arguments and variables of the outer function. The outer function cannot use the arguments and variables of the inner function.
The following example shows nested functions:
When a function contains a nested function, you can call the outer function and specify arguments for both the outer and inner function:
Property Summary
Property
|
Description
|
arguments | An array corresponding to the arguments passed to a function. |
arguments.callee | Specifies the function body of the currently executing function. |
arguments.caller | Specifies the name of the function that invoked the currently executing function. |
arguments.length | Specifies the number of arguments passed to the function. |
arity | Specifies the number of arguments expected by the function. |
constructor | Specifies the function that creates an object's prototype. |
length | Specifies the number of arguments expected by the function. |
prototype | Allows the addition of properties to a Function object. |
Method Summary
Method
|
Description
|
apply | Allows you to apply a method of an object in the context of a different object (the calling object). |
call | Allows you to call (execute) a method of an object in the context of a different object (the calling object). |
toSource | Returns a string representing the source code of the function. Overrides the Object.toSource method. |
toString | Returns a string representing the source code of the function. Overrides the Object.toString method. |
valueOf | Returns a string representing the source code of the function. Overrides the Object.valueOf method. |
Examples
Example 1. The following function returns a string containing the formatted representation of a number padded with leading zeros.
// This function returns a string padded with leading zeros
function padZeros( num, totalLen )
{
var numStr = num.toString(); // Initialize return value
// as string
var numZeros = totalLen - numStr.length; // Calculate no. of zeros
if ( numZeros > 0 ) {
for ( var i = 1; i <= numZeros; i++ ) {
numStr = "0" + numStr;\ } }
return numStr;}
The following statements call the padZeros function.
result = padZeros( 42, 4 ); // returns "0042"
result = padZeros( 42, 2 ); // returns "42"
result = padZeros( 5,4 ); // returns "0005"