Urban pro

View My Profile

Sunday, May 20, 2012

Stepping into JS OOPS - Chapter 1

I was asked yesterday by my friend : why Java Script supports OOP (Object Oriented Programming).

I told him, it is because Java Script  can use inheritance and  objects .

His next Question was : How so ?

So let us see how java script is related to OOP .

JS (java script) can use Objects and objects are very important because they have attributes which can be used

As in java , we can create objects with the new operator.

CREATING OBJECTS :

Inside script-tag :

kObject = new Object(); // creating objects with new operator

kObject.name = "kunal"; //using object attribute name

End of script-tag .

CONSTRUCTOR :

Java uses constructors . Similarly JS can also use constructors .
Inside script-tag :

fruit : function(name) // My function name is fruit, which can have a name and color as its attribute
{
   this.name = name;
   this.typeOfFruit = function();
{
console.log("This is an" +this.name); // console.log is my favorite way of printing a //statement, it is like println() of java and is used in firebug console. you can also use //alert() here .
}
}


fruit1 = new fruit("Apple");
fruit1.typeOfFruit(); // O/P: This is an Apple


End of script-tag .

Here, the method fruit is an object constructor with its own set of attributes and functions.
Objects for this object constructor can be written using the "new" keyword .
Here we did so and inserted the string parameter Apple and then called the function typeOfFruit(), which in turn gets passed on to the function fruit and we get the o/p : This is an Apple.

WHAT  IS THIS ? : 

A lot of times I have been asked : why do we use this inside a  constructor ?

Here let me  take help of Java to explain this :

Consider a  class rectangle. It has its own attributes like height and width .

public  class Rectangle
{
int height = 10;
int width =    5;

// Now here comes my constructor :

public Rectangle(int Height, int Width)
{

 height = Height;
 width = Width;






/* I can describe my constructor in this way as well :


public Rectangle(int height, int width)
{

 this.height = height;
 this.width = width ;




*/
 
}

You can see that I have used the this keyword in my second description of constructor Rectangle.
The first argument of my constructor is height. 
Inside the constructor, a local copy of the same argument needs to be created. 
Hence , we use the keyword this to point to  the field : height .
If i would have used :
height = height ;
I would have got a warning that : this assignment does nothing.
Because we need to assign the variable : height in the constructor to the class attribute : height .

If we are to think of a real time example , consider that you have a very cool looking laptop. But your friend does not know that this laptop is yours . So what do you do ?

You tell him , in front of your dad that , this laptop is yours and he cannot have it .
Here your dad is the class, you are the constructor , who has the object laptop and you want to make sure that you assign it to yourself .


 
 

 


No comments:

Post a Comment