Function

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.

Contents

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.

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.

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.