Javascript - Function

Functions

There are three ways to create a function:

function Add(a, b) { return a + b; }

var Add = function (a, b) { return a + b; } // anonymous function

var Add = new Function(“a”, “b”, “return a + b;”);

Every function object links to Function.prototype. Every function has a prototype property and this property own a consructor property. Like when they are created, it do this.prototype = { constructor: this }.

Add.prototype.hasOwnProperty(‘constructor’); // true

Add.constructor == Function; // true

Add.prototype.constructor == Add; // true

var add = new Add();

add.constructor == Add // true

Invocation

When function is been invoking, it will initiate two additional arguments: this and arguments.

function Add(a, b) { return arguments[0] + arguments[1]; }

- The Method Invocation Pattern

var myObject = {

value: 0;

increment: function (inc) { this.value += typeof inc === ‘number’ ? inc : 1; }

};

myObject.increment(); // myObject.value = 1

myObject.increment(2); // myObject.value = 3

When functions are invoked by property accessor (by . or [key], we said they are invoked as method), the key work this in the method is bound to the function that own the method.

- The Function Invocation Pattern

var sum = add (3, 4);

When functions are invoked as function pattern, the key work this is bound to the global object window. If we like to have a function act as a helper, we could:

myObject.value = 3;

myObject.double = function() {

var that = this;

var helper = function() { add(that.value, that.value); }

helper(); // invoked as function

}

myObject.double(); // myObject.value == 6

- The Apply Invocation Pattern

function Add (a, b) { return a + b; };

var array = [3, 4];

Add.apply (null, array); // 7, the first parameter will be bound to the key work this in the function

Add.call (null, 3, 4); // 7, the first parameter will be bound to the key word this in the function

- The Constructor Invocation Pattern

Add a key word new before the function invocation will return a new object that link to the function’s prototype and the key word this will be bound to this new object.

var Quo = function (string) { this.status = string; };

Quo.prototype.get_status = function () { return this.status };

var myQuo = new Quo (“cool”);

myQuo.get_status() // “cool

Actually, the new statement is equal to:

var myQuo = {};

Quo.call (myQuo, “cool“);

Exceptions

try{ throw { name: “TypeError”, message: “Error message” }; }

catch (e) { window.alert (e.message); }

finally { }

The throw statement just need to throw a normal object.

Recursion

arguments.callee is used to refer to itself in a anonymous function in a recursion.

function Func (){

return function (x){

if(x <= 1) return 1;

return x * arguments.callee (x – 1);

}

}

Closure

We rewrite the example in 2.5.

var quo = function(string) {

var status = string;

return { get_status: function () { return status; } }; // the ‘status’ is a closure

}

var myQuo = quo(“cool”);

myQuo.get_status(); // “cool”

This updated example does not use key word new but use closure.