Featured Post

Inheritance Using JavaScript classes

I am just trying to explain inheritance using Javascript Object .


Step 1:

//Below is Normal animal class delaration which takes name and stuff as
params
function Animal(name,stuff){

this.name=name;
this.stuff=stuff;


}


Step 2:

//Adding Methods to the Animal Class using prototype

Animal.prototype.eat=function(){


alert(" Animal Eats :---->"+this.stuff);
}


Step 3:

//Instantiating Animal Object
var a=new Animal("Horse","Grass");
a.eat();

Output :

(Embedded image moved to file: pic04886.jpg)


Step 4:

//Inheriting properties of Animal to Cat

Cat.prototype=new Animal();

//pointing constructor to your Cat Constructor otherwise it goes to Animal
Constructor

Cat.prototype.constructor=Cat;

function Cat(name,stuff,size)
{
this.name=name;
this.stuff=stuff;
this.size=size;
}


//Overriding Animal Eat method in Cat class

Cat.prototype.eat=function(){

Animal.prototype.eat.call(this); //calling super class eat method as well
alert("Cat Eats:---->"+this.name+":"+this.stuff+":"+this.size);
}

var cc=new Cat("Billi","Mmeat","3");

cc.eat();

Output:

(Embedded image moved to file: pic18875.jpg)


(Embedded image moved to file: pic28433.jpg)


If you have any queries or Comments feel free to send me
mailto:srampv@gmail.com


This e-Mail may contain proprietary and confidential information and is sent for the intended recipient(s) only. If by an addressing or transmission error this mail has been misdirected to you, you are requested to delete this mail immediately. You are also hereby notified that any use, any form of reproduction, dissemination, copying, disclosure, modification, distribution and/or publication of this e-mail message, contents or its attachment other than by its intended recipient/s is strictly prohibited.

Visit us at http://www.polaris.co.in

Comments

Popular posts from this blog

[Inside AdSense] Understanding your eCPM (effective cost per thousand impress...