Wednesday, May 19, 2010

Javascript Tips - Part2

1. Declare Function
http://www.permadi.com/tutorial/jsFunc/index.html
functionName([parameters]){functionBody};

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

assign a variable to an unnamed function: consider the function as an object

var add = function(a, b)
{
    return a+b;
}
var add=function theAdd(a, b)
{
    return a+b;
}
alert(add(1,2)); // produces 3
alert(theAdd(1,2)); // also produces 3

Useful in object oriented program, we can have a function be a property of an object

var myObject=new Object();
myObject.add=function(a,b){return a+b};
// myObject now has a property/a method named "add"
// and I can use it like below
myObject.add(1, 2);


When we declare a function, JavaScript actually creates an object;
We can add properties to Objects, including function objects.

function Ball()     // it may seem odd, but declaration
{                   // creates an object named Ball, and you can
}                   // refer to it or add properties to it like below
Ball.callsign="The Ball"; // add property to Ball
alert(Ball.callsign); // produces "The Ball"

Since function is an object, we can assign a pointer to a function

function myFunction(message)
{
    alert(message);
}
var ptr=myFunction; // ptr points to myFunction
ptr("hello"); // executes myFunction which will prints "hello"


2. Function as Data Type and Function Constructor
http://www.permadi.com/tutorial/jsFunc/index2.html

By declaring a function, we have also created a new data type

function Ball(message)
{
    alert(message);
}
var ball0=new Ball("creating new Ball"); // creates object &
                                         // prints the message
ball0.name="ball-0"; // ball0 now has a "name" property
alert(ball0.name); // prints "ball-0"

The red portion as a shortcut for doing below

function Ball(message)
{
    alert(message);
}
var ball0=new Object();
ball0.construct=Ball;
ball0.construct("creating new ball"); // executes ball0.Ball("creating..");
ball0.name="ball-0";
alert(ball0.name);


Constructor function

function Ball(message, specifiedName)
{
    alert(message);
    this.name=specifiedName;
}
var ball0=new Ball("creating new Ball", "Soccer Ball");
alert(ball0.name); // prints "Soccer Ball"


The "new" keyword eventually causes the constructor function to be executed. In this case, it will executel Ball("creating new Ball", "Soccer Ball"); and the
keyword this will refer to ball0.
Therefore, the line: this.name=specifiedName becomes ball0.name="Soccer Ball".

Every constructor function has a property named prototype.
You do not need to explicitly declare a prototype property, because it exists on every constructor function.

function Test()
{
}
alert(Test.prototype); // prints "Object"


Prototype is an object
when an object is created, the constructor function assigns its prototype property to the internal __proto__ property of the new object.

function Fish(name, color)
{
this.name=name;
this.color=color;
}
Fish.prototype.livesIn="water";
Fish.prototype.price=20;


You can use prototype to assign functions that are common on all objects

function Employee(name, salary)
{
this.name=name;
this.salary=salary;
}
Employee.prototype.getSalary=function getSalaryFunction()
{
return this.salary;
}
Employee.prototype.addSalary=function addSalaryFunction(addition)
{
this.salary=this.salary+addition;
}



No comments:

Post a Comment