[Prev: Font] [Home] [Next: Number]
Functions are normally defined in the application's source code. In some situations it is desirable to create functions at run time.
var min = new Function( "x", "y", "return x < y ? x : y;" );
var m = min( 68, 9 ); // m == 9
The first arguments are the names of the parameters; the last argument is a string which contains the function's definition.
Variable numbers of arguments are also supported, using the arguments array, for example:
max = new Function(
"var max = 0;"
+ "for ( var i = 0; i < arguments.length; i++ ) {"
+ " if ( arguments[ i ] > max ) max = arguments[ i ];"
+ "}"
+ "return max;"
);
System.println( max( 50, 16, 24, 99, 1, 97 ) ); // Prints 99
See also function and function operator.
[Prev: Font] [Home] [Next: Number]