Monthly Archives: August 2009

Driving experience

I did it again last Saturday.. Volunteered to take my Mother to temple in the morning, you know the early morning time, around 6, when driving in the cool wind gives you feeling that heavens can’t be different from this. And add some good soft music, and you know what life is all about.

But the feeling can be engrossing one and you can forget the actual purpose of your drive. Like after traveling for half an hour I released that I was supposed to drive to temple, which I had left like 10 Kms behind.

Not all driving experiences can be same fun, for example my drive from Delhi to Chandigarh yesterday, upto Karnal it was fun, but after that, AC of the car refused to oblige and heat outside was not making a good picture for driving. so last 100 Kms or so were pretty tiring. But again, that is more like life, not always good.

Looking forward to driving back to Delhi this weekend.

Object Oriented JavaScript- 4

We have talked about Basic OO concepts, Objects and Classes, and Inheritance in JS. Today, let me look at polymorphism in JS.

Now polymorphism is a criteria which can help me create multiple method with the same name, with the program deciding at the run time, that which method to be called, based on signature of the method. The signature of methods can differ by number of arguments it takes, or type of arguments. In Js, we can not differentiate between methods based on type of arguments, because every type in JS can be treated as type ‘var’. But yes, JS supports polymorphism using different number of arguments, though indirectly.

In a way, polymorphism in JS is simpler than in OO languages. In JS you don’t even need to create multiple methods, in the same method, you can have different handlers for different number of arguments. arguments.length does the trick for you. Enough of talking, here is your example

<script language=”JavaScript”>
//trying to create a closed figure

function closedFigure()
{

this.createFigure=function()
{
var length = arguments.lengt
h;
if(length<3) { alert("atleast 3 sides are required to close the figure"); } if(length==3) { alert("you are trying to create a triangle with "+ arguments[0]+","+arguments[1]+","+arguments[2]); } if(length==4) { alert("you are trying to create a quadrilateral with "+arguments[0]+","+arguments[1]+","+arguments[2]+","+arguments[3]); } if(length==5) { alert("you are trying to create a pentagon with "+arguments[0]+","+arguments[1]+","+arguments[2]+","+arguments[3]+","+arguments[4]); } } } var figure=new closedFigure(); figure.createFigure(1,2); figure.createFigure(1,2,3); figure.createFigure(1,2,3,4); figure.createFigure(1,2,3,3,5); </script>

Object Oriented JavaScript -3

In the last post I talked about how to create objects and classes in JS. The next step is to go for Inheritance.

Continuing from our previous examples, let’s say you want to create a class for rectangle

function rectangle(length, breadth)
{
this.length=length;
this.breadth=breadth;
this.area=setArea;
this.perimeter=setPerimeter;
this.test=function()
{
alert("hi! you are in test function");
}

function setArea()
{
return (this.length*this.breadth);
}

function setPerimeter ()
{
return 2*(this.length+this.breadth);
}
}

let’s save the above code in a file called rectangleScript.js

Now there is a HTML file where you want to use this JS file code. You will simply add

<script type=”text/javascript” src=”rectangleScript.js” language=”javascript”></script>

Next let’s say you want to add some properties and methods to this already existing class. The ‘prototype’ keyword of Java comes into help here.

<script type="text/javascript" src="rectangleScript.js" language="javascript"></script>
<script language="JavaScript">
rectangle.prototype.color="red";
rectangle.prototype.setColor=function(color)
{
this.color=color;
}

rectangle.prototype.isSquare=function()
{
if(this.length==this.breadth)
return true;
else
return false;
}

var rec=new rectangle(2,6);
alert("area here:"+rec.area());
alert("is it a square:"+rec.isSquare());

alert("color:"+rec.color);
rec.setColor("yellow");
alert("color:"+rec.color);

var rec1=new rectangle(3,3);
alert("area here:"+rec1.area());
alert("is it a square:"+rec1.isSquare());
alert("color:"+rec1.color);

</script>

You might still argue that we have not actually inherited a class by another class, this is just adding properties. Yes, even inheriting a class by another is possible in JS. Again prototype keyword is the magic word. Additionally, we will see how method overloading works with JS. Look at this example

<script type="text/javascript" src="rectangleScript.js" language="javascript"></script>

<script language="JavaScript">

function goldenRectangle(length, breadth, someProperty)
//rectangle with length/ breadth=1.618
{
//this.rectangle=new rectangle(length, breadth);

this.length=length;
this.breadth=breadth;
this.newProperty=someProperty;
}

goldenRectangle.prototype =new rectangle();

//Method Overloading

goldenRectangle.prototype.test=function()
{
alert("hi! you are in goldenRectangles test function");
}

var myGoldenRectangle=new goldenRectangle(10,6,1);
alert("area:"+myGoldenRectangle.area());
alert("test property:"+myGoldenRectangle.newProperty);
myGoldenRectangle.test();
</script>

Rakshabandhan and Future planning

Today is the festival of Rakshabandhan (haan wahi! jab behne bhai ki kalai pe pyaar, i mean rakhi bandhti hai aur unki jeb dhili karti hain). Almost on every year this day, the words of a 7 year old kid comes back to me. Even I was 7 at that time. Yes, some things you just can’t forget (20 saal ho gaye!). So that was the age when you talk to girls only if there is no other option (hoo jee! ladki se baat kar raha tha), and Rakshabandhan was one of those days. Anyways, girls were tying Rakhi to all the boys, and this dude, my friend at that time, was hiding in a corner. I asked him “Kyu be! tu kyu nahi bandhwa raha rakhi” and the reply was “Abey! rakhi bandhwaonga to ye meri behan ho jayegi. aur agar bade ho ke inme se kisi se meri shadi ho gayi to?”

Dude! that is some serious future planning. I am pretty sure this Guy has made big in his life.

Object Oriented JavaScript -2

Continuing from my first post about this topic, where I mentioned why we need OO JS. First of all let me see what do we picture the moment we say that a language is OO. Here are some basic necessities of OO
Encapsulation: Usage of Classes and Objects
Inheritance: Extending Classes, Overloading methods
Polymorphism: Same method with different number of arguments

Now without wasting further time, lets get on with how these OO principles can be applied to JS

Creating Objects in JS

1. Using Inbuilt Object type:

var myRectangle=new Object();
myRectangle.length=2;
myRectangle.breadth=4;

myRectangle.area=function()
{
return (myRectangle.length*myRectangle.breadth);
}

alert(“area:”+myRectangle.area());

2. The JSON (JavaScript Object Notion) way
var myRectangle2={
length:2,
breadth:4,
area:function()
{
return(this.length*this.breadth);
}
};

alert(“area2:”+myRectangle2.area());

Creating Classes in JS

You might argue that so far we have only created Objects, but in an OO language we first create a class and then create object. Well, that is very much possible in JS as well.

The good news is, that in JS, functions can be treated as classes. How? Let’s check this example.

//Creating a class in JS
function rectangle(length, breadth)
{
this.length=length;
this.breadth=breadth;
this.area=setArea;
this.perimeter=setPerimeter;

this.test=function()
{
alert(“hi! you are in test function”);
}

function setArea()
{
return (this.length*this.breadth);
}

function setPerimeter ()
{
return 2*(this.length+this.breadth);
}
}

var rec=new rectangle(2,6);
var areahere=rec.area();
alert(areahere);
alert(rec.perimeter());

var rec1=new rectangle(1,1);
var areahere=rec1.area();
alert(areahere);
alert(rec1.perimeter());
rec1.test();

More on OO JS (Inheritance and Polymorphism) later