Javascript functions, methods


A function statement creates a function. The parameters are separated by commas and surrounded in parens. The body of the function is surrounded in curly braces:

// create a function 'foo' with parameters 'a' and 'b'
function foo(a, b) {
    // ... function body goes here

A function call is denoted with the function name before the open paren, and the arguments are separated by commas:

foo(6, "hi", 2)        // call function 'foo' with arguments 6, "hi", and 2

If a function does not explicitly return a value with a return statement, it implicitly returns the value undefined.

Like in Pigeon, functions are values: we can assign them to variables, pass them into functions, and return them from functions. An anonymous function is an expression returning a new function. It looks just like a statement defining a function, but we don’t have to specify a name after function:

var foo = (function(a, b) {
    // ...

(The parens surrounding the function are not needed, but I include them to emphasize that this is an expression. Also note the semi-colon at the end.)

A function statement is really just shorthand for declaring a variable and assigning it an anonymous function:

// just like the previous example, this creates a variable 'foo' and assigns it a new function
function foo(a, b) {
    // ...

We can call a function with extra arguments with no error. Every function automatically has a local variable arguments which is assigned an array containing every argument to the call:

function foo(a, b) {
    var sum = 0;
    for (var i = 0; i < arguments.length; i++) {
        sum = sum + arguments[i];
    return a + b + sum;

foo(1, 2);                // 3     (1 + 2)
foo(1, 2, 3, 4);          // 10    (1 + 2 + 3 + 4)

If we call a function with too few arguments, the parameters given no arguments have the value undefined:

function foo(a, b) {
    return b;

foo(3);                   // undefined


Like any other value, we can assign a function to keys of an object. When invoking a function via an object property with the . operator, the object itself is passed to a special parameter called this:

var x = {}; = function() {
    return this;         // return value of the special parameter 'this'
};;                 // the value of 'x' is passed to 'this'

In this arrangement, the function is sometimes called a method of the object.

If a function that uses the special parameter this is called as a function rather than a method, this is passed an object that represents the global namespace of the program:

var bar = 9;                // a global
function foo() {
    return + 2;
foo();                      // 11

(This behavior is not useful: it’s simply a mistake in Javascript’s design! If a function uses this, it should only be called as a method.)

If we invoke a property which is not a function or does not exist, we get an exception:

var x = {foo: 9};;                     // exception: is a number, not a function;                     // exception: is undefined, not a function