The JavaScript Function Object


The JavaScript Function Object

Believe it or not, JavaScript can treat functions as data typeswith the result that you can declare a function as a variable. You can do this with the JavaScript Function object.

Here's an example showing how this works. In this case, I'll create a function named adder that will take two operands, which I'll name operand1 and operand2 , add them, and return their sum. You can do that with a new Function object as follows . (Recall that you have to use the new keyword when creating a new object.)

 var adder = new Function("operand1", "operand2", "return operand1 + operand2") 

Here, I pass the name of the first operand, then the second operand, and then the JavaScript code for the function. (You can specify as many function parameters as you wantup to 255 in JavaScriptnot just the two I've used here.) Now you can use this new function as you would any other:

 var adder = new Function("operand1", "operand2", "return operand1 + operand2")  document.write("2 + 3 = " + adder(2, 3))  

The Function object has been available since Netscape Navigator version 3.0 and Internet Explorer version 3b, as you see in Table 3.11. (Earlier, unreliable versions of this object were available in Netscape Navigator version 2.0 and Internet Explorer version 3a.) You can find the properties of this object in Table 3.12 and the methods of this object in Table 3.13. (It has no events.)

Table 3.11. The Function Object

Object

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

Function

 

x

x

x

 

x

x

x

x

x

Table 3.12. Properties of the Function Object

Property

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

arguments

 

x

x

x

 

x

x

x

x

x

   

An array holding the arguments passed to a function.

arguments.callee

 

x

x

x

 

x

x

x

x

x

   

Holds the function body of the executing function.

arguments.caller

 

x

x

x

 

x

x

x

x

x

   

Holds the name of the function that called the currently executing function.

arguments.length

 

x

x

x

 

x

x

x

x

x

   

Holds the number of arguments passed to the function.

arity

 

x

x

   

x

x

x

x

x

   

Holds the number of arguments that were expected by the function.

constructor

 

x

x

x

 

x

x

x

x

x

   

Gives the function that creates an object.

length

   

x

x

 

x

x

x

x

x

   

Holds the number of arguments expected by a function.

Table 3.13. Methods of the Function Object

Method

NS2

NS3

NS4

NS6

IE3a

IE3b

IE4

IE5

IE5.5

IE6

apply

     

x

       

x

x

   

Enables you to apply a method of another object while inside a different object.

   

Syntax: apply([obj[, argumentsArray]])

call

     

x

       

x

x

   

Enables you to call a method of another object while inside a different object.

   

Syntax: call(obj[, arg1[, arg2[...argN]]])

toSource

   

x

x

   

x

x

x

x

   

Returns a string representing the source code of the function.

   

Syntax: Object.toSource()

toString

   

x

x

   

x

x

x

x

   

Returns a string representing the source code of the function.

   

Syntax: Object.toString()

valueOf

   

x

x

   

x

x

x

x

   

Returns a string holding the actual source code of the function.

   

Syntax: Object.valueOf()



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net