OBJECT:
Function
new
Function([arg1[, arg2[, ... argN]],] functionBody)
The Function object permits a function to have methods and
properties associated with it. To accomplish this, the function is temporarily
considered to an object whenever you wish to invoke a method or read
the value of a property.
Note that JavaScript treats the function, itself, as a data type that
has a value. To return that value, the function must have a return
statement.
When a Function object is created by using the Function
constructor, it is evaluated each time. This is not as efficient as
the alternative method of declaring a function using the function
statement where the code is compiled.
The following example creates a Function object to calculate
the average of two numbers, and then displays that average:
Code:
var twoNumAverage = new Function("x", "y", "return
(x + y)/2")
document.write(twoNumAverage(3,7))
The Function object can be called just as if it were a function
by specifying the variable name:
Code:
var average = twoNumAverage(12,17)
If a function changes the value of a parameter, this change is not
reflected globally or in the calling function, unless that parameter
is an object, in which case any changes made to any if its properties
will be reflected outside of it. In the next example, an object called
TaxiCo is created with a property containing the size of the taxi fleet.
A Function object called AddCar is then created which allows
a user to alter the size of the Fleet property to reflect an increase
in the number of cars, and then an instance of this function adds 2
to the Fleet property with the final line of code displaying the new
size of the taxi fleet:
Code:
taxiCo = {name:"City Cabs", phone:"321765", fleet:17}
var addCar = new Function("obj", "x", "obj.fleet = obj.fleet + x")
addCar(taxiCo, 2)
document.write("New fleet size = " + taxiCo.fleet)
Output:
New fleet size = 19
The function in the above example could also be created by declaring
it using the function statement as follows...
function addCar(obj,x){obj.fleet = obj.fleet + x}
...the difference being that when you use the Function constructor,
AddCar is a variable whose value is just a reference to the function
created, whereas with the function statement AddCar is not a
variable at all but the name of the function itself.
You can also nest a function within a function in which case the inner
function can only be accessed by statements in the outer function. The
inner function can use arguments and variables of the outer function,
but not vice versa. The following example has an inner function that
converts a monetary value from Pounds Sterling into Dollars. The outer
function takes four values (the first two in Dollars and the second
two in Pounds), passes each of the Pound values to the inner function
to be converted to Dollars, and then adds them all together returning
the sum:
Code:
function totalDollars(v,w,x,y)
{
function convertPounds(a)
{
return a * 1.62
}
return v + w + convertPounds(x) + convertPounds(y)
}
document.write("Total Dollars = " + totalDollars(400, 560, 250, 460))
Output:
Total Dollars = 2110.2
A Function object can also be assigned to an event handler
(which must be spelled in lowercase) as in the following example:
Code:
window.onmouseover = new Function("document.bgColor='lightgreen'")
A Function object can be assigned to a variable, which in turn
can then be assigned to an event handler, provided it doesn't take any
arguments, because event-handlers cannot handle them. In the following
example a function which changes the background color to green is assigned
to a variable ChangeBGColor. This in turn is assigned to an onMouseOver
event connected to an anchor in a line of text:
Code:
<script language="javascript">
var changeBGColor = new Function("document.bgColor='lightgreen'")
</script>
He turned <a name="ChangeColor" onMouseOver="changeBGColor()">green
</a> with envy.
PROPERTIES
arguments Property
The arguments property, which is now deprecated, consists of
an array of all the arguments passed to a function.
Syntax: [Function.]arguments
arguments.callee Property
The arguments.callee property can only be used within the body
of a function and returns a string specifying what that function is.
Syntax: [Function.]arguments.callee
arguments.caller Property
The arguments.caller property is deprecated in JavaScript 1.3
and is no longer used, but where it is, it specifies the name of the
function that called the currently executing function.
Syntax: [Function.]arguments.caller
arguments.length Property
The arguments.length property returns the number of arguments
passed to a function.
Syntax: [Function.]arguments.length
arity Property
The arity property specifies the number of arguments expected
by a function. It is external to the function and is in contrast to
the arguments.length
property which specifies the number of arguments actually passed to
a function.
Syntax: [Function.]arity
Compare the length property below.
constructor Property
The constructor property specifies the
function that creates an object's prototype. It is a direct reference
to the function itself rather than a string containing the function's
name. See the constructor property
of the Object object for more details
and examples.
Syntax: Function.constructor
length Property
The length property specifies the number of arguments expected
by a function. It is external to the function and is in contrast to
the arguments.length
property which specifies the number of arguments actually passed to
a function. Compare the arity property above.
Syntax: Function.length
prototype Property
The prototype property is a value from which all instances of
an object are constructed, and which also allows you to add other properties
and methods to an object. See also the prototype
property of the Object object for more
details and examples.
Syntax: Function.prototype.name = value
METHODS
apply Method
The apply method allows you to apply to a function a method from
another function.
Syntax: Function.apply(thisArg[, argArray])
call Method
The call method allows you to call a method from another object
Syntax: Function.call(thisArg[, arg1[, arg2[, ...]]])
toSource Method
The toSource method creates a string representing the source
code of the function. This over-rides the Object.toSource
method
Syntax: Function.toSource
()
toString Method
The toString method (like the valueOf method below) returns
a string which represents the source code of a function. This over-rides
the Object.toString method.
Syntax: Function.toString
()
valueOf Method
The valueOf method (like the ToString method above) returns
a string which represents the source code of a function. This over-rides
the Object.valueOf method.
Syntax: Function.valueOf
()
|