Javascript – functions

Posted: June 19, 2010 in Javascript
Tags: , ,

//Note – You can directly test this post by copying everything 2 a .js file and refer it in .htm page

// function in JS are objects linked to function.prototype which itself linked 2 Object.prototype
// Every fn is created with 2 additioinal hidden properties – fn’s context, code that implements the fn’s behavior
// Function literal
var add = function (a, b) {
return a + b;
};

// Along with declared parameters fns receive 2 more parameters as arguments – this & arguments
// You can excess or less arguments while calling function than the defined arguments, you can use arguments to get all parameters in case of less
// parameters it will give undefined when accessing the non passed parameters
// function if it does not return anything then it will return “this”

var sum = function () {
var sum = 0; // name does not interfere with the variable defined outside
for (i = 0; i < arguments.length; i++) // arguments is not an array but is array like obj, it has length property but it lacks all other array methods
sum += arguments[i];
return sum;
}

document.writeln(sum(2, 3, 4, 6, 7, 8) + “<br/>”);

// 4 patterns function invocation
// 1. Method invocation pattern
var myObject = {
value : 0,
increment : function (inc) {
this.value += typeof inc === ‘number’ ? inc : 1;
}
};

myObject.increment();
document.writeln(myObject.value + “<br/>”);

myObject.increment(2);
document.writeln(myObject.value + “<br/>”);

// 2. Function invocation pattern – when a fn is not the property of an object
var newsum = add(3, 4);

// This kind of function is bound 2 global obj (mistake in design of lang)
// due to this mistake “this” can not be used in the inner function to access stuff of the employed function
// workaround is assign this to a variable in the main function

// Augment myObject with a double method
myObject.double = function () {
var that = this;
var helper = function () {
that.value = add(that.value, that.value);
}
helper();
};

myObject.double();
document.writeln(myObject.value + “<br/>”);

// 3. Construction invocation – by creating constructor fn (fns that are intended 2 be used with the new prefix are called constructors)
// JS is a prototypal inheritance lang i.e. objs can inherit properites directly from other objects which makes js as class-free
// If a fn is invoked with the new prefix then a new object will be created with a hidden link to the value of the fn’s prototype member
// & this will be bound 2 that new obj

//  By convention use capital start letter for variable
var Quo = function (string) {
this.status = string;
}

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

var myQuo = new Quo(“confused”);

document.writeln(myQuo.get_Status() + “<br/>”);

// 4. Apply invocation pattern
// bcos js is a functional oo lang, fns can methods

// Apply lets us construct an array of arguments 2 use 2 invoke a fn. Apply takes 2 arguments, first one we can specify value for “this”, second is array of params

var array = [3, 4];

document.writeln(add.apply(null, array) + “<br/>”);

var newStatusObj = {
status : “called thru apply”
};

// newStatusObj does not inherit from Quo.prototype but we can invoke get_status method on newStatusObj eventhough it does not have it
document.writeln(Quo.prototype.get_Status.apply(newStatusObj) + “<br/>”);

// Exception handling – throw {}, try {}catch(e) {}

var newadd = function (a, b) {
if (typeof a !== ‘number’ || typeof b !== ‘number’)
throw { name: ‘TypeError’, message: ‘pass numbers’ };
return a + b;
};

var try_it = function () {
try {
newadd(‘fool it’);
} catch (e) { document.writeln(e.name + “: ” + e.message + “<br/>”); }
};

try_it();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s