JavaScript » Objects » 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.


var twoNumAverage = new Function("x", "y", "return (x + y)/2")

This example creates a Function object to calculate the average of two numbers, and then displays that average:

var average = twoNumAverage(12,17)

The Function object can be called just as if it were a function by specifying the variable name.

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)
New fleet size = 19

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 this 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.

function addCar(obj,x){obj.fleet = obj.fleet + x}

The function in the above example could also be created by declaring it using the function statement like this. The difference is 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.

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))
Total Dollars = 2110.2

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. This 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.

window.onmouseover = new Function("document.bgColor='lightgreen'")

A Function object can also be assigned to an event handler (which must be spelled in lowercase) as in this example.

<script type="text/javascript">
var changeBGColor = new Function("document.bgColor='lightgreen'")
He turned <a name="ChangeColor" onMouseOver="changeBGColor()">green </a> with envy.

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 this 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.



Syntax: [Function.]arguments

The arguments property consists of an array of all the arguments passed to a function.


Syntax: [Function.]arity

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: Object.constructor

This specifies a function to create an object's property and is inherited by all objects from their prototype.


Syntax: Function.length

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: = value

This allows the addition of properties and methods to any object.



Syntax: Function.apply(thisArg[, argArray])

The apply method allows you to apply to a function a method from another function.


Syntax:[, arg1[, arg2[, ...]]])

The call method allows you to call a method from another object


Syntax: Object.eval(string)

The eval method is deprecated as a method of Object, but is still used as a high level function. It evaluates a string of JavaScript in the context of an object.


Syntax: Object.toSource()

The toSource method returns a literal representing the source code of an object. This can then be used to create a new object.


Syntax: Object.toString()

The toString method returns a string representing a specified object.


Syntax: Object.unwatch(property)

This method removes a watchpoint set for an object and property name with the watch method.


Syntax: Object.valueOf()

This method returns a primitive value for a specified object.


Syntax:, handlerfunction)

This method adds a watchpoint to a property of the object.