HTML and CSS Reference
In-Depth Information
Table 9.14 Methods of the Function Object
Property
What It Does
apply()
Allows you to apply a method from one function to another.
call()
Allows you to call a method from another object.
FORMAT
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
document.write(addemUp(10, 5));
EXAMPLE 9.34
<html>
<head><title>Function Object</title></head>
<body bgcolor=lightgreen>
<font face="arial" size="+1">
<center>
Anonymous Functions and the Function Constructor<p>
<script type="text/javascript">
1
var sum = new Function("a","b", "return a + b; ");
2
window.onload = new Function ( "document.bgColor='yellow';");
3
document.write( "The sum is " + sum(5,10) + "<br />");
document.write( "The background color is yellow<br />");
</script>
</font>
</body>
</html>
EXPLANATION
1
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.
2
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.
3
The sum function is called with two arguments (see Figure 9.39).
 
Search WWH ::




Custom Search