JavaScript Wiki
Advertisement

A function is a list of instructions which can be executed ("called") at any point in its lifetime. A function literal looks like this:

function <name>(<parameters>){
    <statements>
}

All three syntax elements (name, parameters, and statements) are completely optional; however, a function is worthless if it does not contain any statements. The need for a name and parameter list depend upon the intended use of the function. If a function is not given any name, it is anonymous.

name[]

A function is an ordinary value, so it does not necessarily need to be named; naming is actually just a convenient way to assign functions. A common and justified idiom follows which assigns an anonymous function as an event-handler.

window.onload = function (){
    window.alert('Hello, world!');
};

Compare that to this (which works in Firefox but not in Internet Explorer):

function onload(){
    window.alert('Hello, world!');
}

But named functions have their advantages too, mostly in the best practices arena. So how can you decide which type of function assignment you should use? We must first understand their differences.

Ordinary Assignment[]

The first example above uses ordinary assignment to work around an implementation difference.

Function Assignment[]

A named function automatically behaves as if it were declared with "var," so there is no chance of a scope bug. It is defined from the beginning of its immediate scope, regardless of its exact position.

Works Breaks
f();
function f(){}
f();
var f = function (){};

The Function.name property is automatically set to the given name.


Members[]

These properties and methods are available on the global Function constructor.

prototype[]

Data type object
Standard ECMA-262 §15.2.3.1
Documentation Mozilla, Microsoft

These members are available on each instance of Function.

apply[]

Data type function
Return type this function's result
Parameter list object (anything), args (array)
Standard ECMA-262 §15.3.4.3
Documentation Mozilla Microsoft

Calls this function with the given arguments and as a method of the given object (i.e., with the object as the value of this). Differs from call only in how the arguments are passed. Note that args may be any array-like object - for example, the arguments variable available to every function.

call[]

Data type function
Return type this function's result
Parameter list object (anything), args... (any number of values)
Standard ECMA-262 §15.3.4.4
Documentation Mozilla Microsoft

Calls this function with the given arguments and as a method of the given object (i.e., with the object as the value of this). Differs from apply only in how the arguments are passed.


Parameters[]

The parameters of the function are wrapped in parentheses and separated by commas.

var myFunction=function (a, b){
    return a+b;
}

They are passed to the function when it is invoked :

alert(myFunction(1,2)); //Will return 3.

You can omit parameters :

myFunction(1);

...but you have to add a way to deal with the missing parameter in the function itself :

var myFunction=function (a, b){
    if(b===undefined){
    b=0;
    }
    return a+b;
}
Advertisement