ECMA-262 Core: JavaScript Function Literals



Navigation Aids -- This Page   Navigation Aids -- This Topic   Navigation Aids -- This Site








Introduction And Review

Recall that the literal function statement is one of three approaches for declaring a function. The other two approaches are:

  1. The JavaScript Function Statement Declaration
  2. The Function Class Constructor

The function literal statement has similarities with each of the other two approaches. The comparison of these three declaration approaches are at our section entitled, Comparing the Characteristics of the Three Approaches.



Common Characteristics to all Declaration Approaches:



Characteristics Specific to a Function Literal



Examples of Literals for Composite Types

The three composite data types associated with JavaScript each have a literal syntax for declaring an object of there class type. Here is a brief review of the three composite data types literal forms:

Composite Type Literal Syntax
Type Syntax Example Comment/Links
Footnote All examples of the above literals are shown with an assignment statement. The actual literal is the code on the right side of the assignment operator.
Array square brackets
[ ]
var primes = [2,3,5,7]; the array literal is: [2,3,5,7]
Array Class Constructor Alternative
Function see note 01 below var area = function(x,y)
{ return x*y; };
uses the function keyword
Function Class Constructor Alternative
Object see note 02 below var myobject = {
fname: "Fritz",
lname: "Fry",
add1: "12345 S. Street",
city: "Town",
state: "CA",
zip: null
}
Object Class Constructor Alternative



Note 01: The function literal begins with the function keyword. Following the keyword, the function literal consists of a comma separated list of parameter names that are all enclosed by a set of parentheses. This is followed by the function body statements enclosed by a set of curly braces.

Note 02: The object literal consists of property specifications, each separated by commas and all enclosed by a set of curly braces. Each property specification consists of the property name followed by a colon and finally the property value.



Top            

Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson