The function OperatorBesides the Function object, JavaScript also supports a function operator . Following the lead of languages such as Perl and Java, the function operator enables you to define a function without a namethat is, an anonymous function. Here's the syntax of this statementnote how much this works like just assigning a function definition to a variable: var variableName = function([ parameter1 [, parameter2 ...[, parameterN ]]]) { functionBody } As you see in Table 3.14, the function operator is fairly recent. Table 3.14. The function Operator
The syntax for using this operator is much like that for using the Function object. Here's an example putting this operator to work, where I'm setting up a function to double the number you pass it, and assigning that function to a variable named doubler : (Listing 03-15.html on the web site)<HTML> <HEAD> <TITLE> Passing Data to Functions </TITLE> </HEAD> <BODY> <H1>Passing Data to Functions</H1> <SCRIPT LANGUAGE="JavaScript"> <!-- var doubler = function(operand1) {return 2 * operand1} document.write("2 * 10 = " + doubler(10)) // --> </SCRIPT> </BODY> </HTML> Figure 3.12 shows the results of this code. Figure 3.12. Using the function operator.
|