Fandom

JavaScript Wiki

Function

87pages on
this wiki
Add New Page
Talk0 Share

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.

nameEdit

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 AssignmentEdit

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

Function AssignmentEdit

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.


MembersEdit

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

prototypeEdit

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

These members are available on each instance of Function.

applyEdit

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.

callEdit

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.


ParametersEdit

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;
}

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.