The Function object in JavaScript and also a discussion about functions in JS in general.

Basic Usage

Function. A function is a defined sequence of statements (the function body) that does work and is usually invoked by calling upon it. In JS, functions are first-class functions, i.e. are objects that can be created, modified, manipulated, passed, assigned, etc., just like any other object. There are three ways to define a JavaScript function:

Parameters. Zero or more (up to 255 in JS) arguments (parameters) are passed into the function when it is called upon. Non-object parameters (strings, numbers, and booleans) are passed in by value (a change in the paramter inside the function does not affect the value of the parameter outside of the function). Object parameters (objects, arrays, regular expressions) are passed in by reference (a change in the parameter inside the function affects the value of the pparameter outside of the function).

Return. A function may return a value back to the location where the function was invoked. Since JS is loosely-typed, the type of the returned value may vary. In order for a function to return a value, the return [expression] statement must be used within the function. return will also stop execution of the function. If the return statement is not used, or return void(0) is used, then the function returns a value of undefined. The return statement is NOT white-space insensitive:

//This return returns the object
return {
MyObject }

//This return returns undefined!
return 
{ MyObject }

Recursive. A recursive function is a function that calls itself. In JS, a recursive function has 3 ways to call itself: 1. The function's name. 2. arguments.callee(). 3. An in-scope variable that refers to the function. EG:

function WalkTree(node){
    if (node==null) return;
    for(var i=0; i<node.childNodes.length; i++){
        WalkTree(node.childNodes[i]);
        //arguments.callee(node.childNodes[i]); //alternative
    }
}

Nesting. You can nest functions but the inner function is private to the outer function. One quirk is that when you call a function with an inner function, you can actually specify arguments for its inner function. EG: x = outerfun(varForOuter)(varForInner).

Calling. While a function or method is usually called or run sort of like this: var x = AFunction();, it is also possible to use a function operator or epxression to make an anonymous function which is immediately run by simply placing the parentheses next to it. EG: var y = function(){ return {a:1, b:'hi'}; }();. In the example if the last pair of parentheses were not there, the y would be the method, but intead with the parentheses, y is an object with two properties: a and b.

A few other examples of functions in JavaScript.

function double(a) { //A function statement that can be used directly
    return a+a;
}
alert(double(2)); //Returns 4
function sayIt(){ //A function statement that is added as a method into objects below
	return "I am " + this.name;
}

function Dog(name, breed) { //This function statement is a constructor for a Dog prototype
    this.name = name; //Property of Dog
    this.breed = breed; //Property of Dog
    this.say = sayIt; //Method of Dog added via a object reference. Note the absence of parentheses.
    this.bark = function() { //Method of Dog added via function operator. The function name is optional.
        return "Bow Wow!";
    }
}
myDog = new Dog("Spot", "Greyhound"); //This instantiates the object myDog 
alert(myDog.say()); //Says "I am Spot"
alert(myDog.bark()); //Says "Bow Wow!"
myCat = {name:"Tom",say:sayIt}; On the fly object created using JSON
alert(myCat.say()); //Says "I am Tom"

Properties

Methods

These methods are inherited from Function.prototype: .apply(), .call(), .toSource(), .toString(), .valueOf().

These methods are inherited from Object.prototype: __defineGetter__(), __defineSetter__(), hasOwnProperty(), isPrototypeOf(), __lookupGetter__(), __lookupSetter__(), __noSuchMethod__(), propertyIsEnumerable(), unwatch(), watch().

Top-Level Functions

These are top level functions that are built into JavaScript and are not associated with any objects.

encode or escape Related

These functions are most often used for data going to or from an URL, URI, query string, etc..

Miscellany

As a side, here are the Top-Level Properties:

2008-03-17 22:30:42Z