HTML and CSS Reference
Table 9.14 Methods of the Function Object
What It Does
Allows you to apply a method from one function to another.
Allows you to call a method from another object.
var nameOfFunction = new Function (arguments, statements_as_string: }
EXAMPLE F UNCTION D EFINITION
var addemUp = new Function ( "a", "b", "return a + b;" );
EXAMPLE F UNCTION C ALL
<font face="arial" size="+1">
Anonymous Functions and the Function Constructor<p>
var sum = new Function("a","b", "return a + b; ");
window.onload = new Function ( "document.bgColor='yellow';");
document.write( "The sum is " + sum(5,10) + "<br />");
document.write( "The background color is yellow<br />");
A variable called sum is a Function object, created by the Function() constructor.
It has two arguments, “a” and “b” . The function statements are the last string in
the list. These statements will be executed when the function is called.
This Function() constructor only has one argument, the statement that will be ex-
ecuted when the function is called. Because the function is assigned to the onload
event method for the window object, it will be invoked when the window has fin-
ished loading and cause the background color to be yellow.
The sum function is called with two arguments (see Figure 9.39).