• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

4. Variables, Functions, and Flow Control > 4.4. Creating an Anonymous Function

Creating an Anonymous Function

NN 4, IE 4

Problem

You want to define a function in the form of an expression that you can, for example, pass as a parameter to an object constructor or assign to an object’s method.

Solution

You can use an alternate syntax for defining functions without creating an explicitly named function (as shown in Recipe 4.2). Called an anonymous function, this syntax has all the components of a function definition except its identifier. The syntax model is as follows:

var someReference = function( ) {statements go here};

Statements inside the curly braces are semicolon-delimited JavaScript statements. You can define parameter variables if they’re needed:

var someReference = function(paramVar1[,..., paramVarN]) {statements go here};

PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint