JavaScript Wiki
A page in the
 
Introduction
 
Basics
Statements · Control Flow Statements · Comments · Objects · Functions · Style
Features
Scoping · Inheritance · DOM


It has been suggested that this page or section be merged into Wikibooks:JavaScript. (Discuss)

A function contains a section of a program, i.e. a list of statements, which can be addressed by a specified name, or "called," independent of the rest of the program. A function's statements, known as its "body," will only be executed when it is called. To call a function, simply append parentheses () to its name.

Each function may define parameters - variables which, when the function is called, hold the corresponding values "passed" (assigned) in the function call. Those corresponding values are known as arguments, and they are only stored in the parameters within the function's body.

It may also return a value which a simple function call will be evaluated as. If no value is returned, the call evaluates to undefined.

function name(parameter1, parameter2, etc){
    statements;
    return value; //optional
}
value = name(argument1, argument2, etc);

The usefulness of objects, and indeed JavaScript itself, is extremely limited without functions. In fact, many of the built-in objects may only be obtained via function calls.

JS Examples[]

Methods[]

When a function is assigned and called as a property of another object, it is known as a "method." Within a method, the keyword this refers to the function's "owner" - the object to which it is assigned as a property.

function name(){
    this.num++;
    return this;
}
var obj = {
    num: 0,
    inc: name
};
obj.inc().inc().inc();

At the end of this program, obj is {inc: name, num: 3} because the inc method returns its owner.

Anonymous functions[]

Anonymous functions, i.e. those defined without names, are almost exclusively used as methods. We can get rid of an unused variable by applying this to our program:

var obj = {
    num: 0,
    inc: function (){
        this.num++;
        return this;
    }
};
obj.inc().inc().inc();

Constructors[]

A function call prefixed by new generates or, more accurately, "constructs" a new object. Functions used for this purpose are called "constructors," and their names are typically capitalized to aid humans in identifying them correctly. The act of calling a constructor with new is known as "instantiating" the constructor, and the resulting object is an "instance" of the constructor. Inside the constructor's body, this is the current instance of the constructor.

function Counter(){
    this.num = 0;
    this.inc = function (){
        this.num++;
        return this;
    };
};
var obj = new Counter();
obj.inc().inc().inc();

Prototypes[]

Until the constructor is executed, each instance is exactly identical to the prototype. This technique is emphasized in Douglas Crockford's Prototypal Inheritance.

function Counter(){}
Counter.prototype = {
    num: 0,
    inc: function (){
        this.num++;
        return this;
    }
};
var obj = new Counter();
obj.inc().inc().inc();

Factory methods[]

A factory method instantiates a constructor, slightly alters the instance, and returns it. Douglas Crockford's parasitic inheritance is an extreme example of this.

Permissions[]

Any variables declared with var inside a function will not be available from outside the same function. Each similar variable from outside the function will be available unless it shares its identifier with a local variable. Let us assume that, for some reason, we want the current count to be normally inaccessible to the calling program. We can move both it and the function to the inside of the constructor along with a new "getter" - a function which simply returns a private value. As Douglas Crockford refers to such variables[1], the count will be "private," the functions as above were "public," and the functions will now be "privileged" (public but informed of private variables).

function Counter(){
    var num = 0;
    this.inc = function (){
        this.num++;
        return this;
    };
    this.getNum = function (){
        return num;
    };
}
var obj = new Counter();
obj.inc().inc().inc();

Distinct features[]

JavaScript functions are different from those of many languages because they:

First-class functions[]

First-class functions are objects; they possess all the same behaviors of other objects, plus a few extras.

Variable arity[]

Variable arity is the fancy term used to characterize functions which can fill their formal parameters and take any number of additional arguments. JavaScript also allows a function's formal parameters to be ignored in calls to that function; in such a case, the empty parameters are set to undefined.

Closures[]

Closures allow one function B to access the scope of another function A, even if A is no longer executing. However, this requires that B was originally defined within A.

Dynamic binding[]

Dynamic binding occurs when a function assigned as a method on one object is also assigned to another object. In this case, which object does this refer to? According to JavaScript, whichever served as the function's namespace for any given call is this.

References[]