PART IV
Appendix A: JavaScript Quick Reference 565
Function Literals
Function literals are used with the following syntax:function ([ args ])
{
statements
}where args is a comma-separated list of identifiers for the function arguments, and statements
is zero or more valid JavaScript statements. Function literals are often found in constructors:function Dog()
{
this.bark = function () {alert("woof!"); };
}or when binding them to event handlers:xhr.onreadystatechange = function () { alert("do something!"); };or performing other higher-order programming tasks.
Although not strictly a literal, you can also use the Function() constructor.new Function(["arg1", ["arg2"], ... ,] "statements");The argNs are the names of the parameters the function accepts, and statements is the
body of the function. For example:myArray.sort(new Function("name", "alert('Hello there ' + name) "));Regular Expression Literals
Regular expression literals (actually RegExp literals) have the following syntax:/exp/flagswhere exp is a valid regular expression and flags is zero or more regular expression modifiers
for example, “gi” for global and case-insensitive.
Although not strictly a literal, you can use the RegExp() constructor inline in
JavaScript.new RegExp("exp" [,"flags"])Identifi ers
JavaScript identifiers start with either a letter, underscore, or dollar sign and can be followed
by any number of letters, digits, underscores, and dollar signs. Given this, the following are
legal identifiers being used as variables:var myName = "Thomas";
var x = 33;
var _pleaseNo = true;
var $$$$ = "Big Money!";