Skip navigation

The code below pretty much explains itself. Just follow the comments. You can also run the code snippet in the firefox console tab

// Parent class definition
var Human = function(name, age) {
    this.name = name;
    this.age = age;
}

// Child class definition
var Indian = function(name, state){
    if(name) this.name = name;
    this.state = state;
}

// Extending
Indian.prototype = new Human('raja',22);

// Creating instance of child class with own name property
var tamilian = new Indian('kvk','TN');

// Creating another instance of child class without own property
var cbe_ian = new Indian('','TN');

alert(tamilian.name);  // kvk
alert(tamilian.age);   // 22
alert(tamilian.state); // TN
alert(cbe_ian.name);   // raja
Advertisements

3 Comments

  1. Shouldn’t tamilian.name be ‘kvk’ and cbe_ian.name be ‘raja’ ?

  2. Thanks for updating

  3. Good Raja


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

%d bloggers like this: