JavaScript - An Example for Inheritance

Everyone of us know, Inheritance is a way to acquire properties and methods of another object. In other words it is a way to establish is-a relationship between two objects. The static languages by default provide some easier ways to achieve this. For an example, we have "extend" keyword in Java for extending other object.

JavaScript is protypal inheritance language (class-free) means that objects can inherit properties from other objects. Let's see an example.

Let's create a parent class for Animal

/**
 * Animal - Constructor and parent class for all animals.
 * @param {Object} aType - Type of the animal
 * @param {Object} aName - Name of the animal 
 */

function Animal(aType, aName) {
 this.type = aType;
 this.name = aName;
}

Animal.prototype.eat = function() {
 console.log('All animal will eat');
};

We have to do following steps for extending animal class.

1. Calling super class constructor from child class constructor.
2. Setting up prototype chain.
3. Adding methods to child class.

Let's create child class and extending Animal class by following above steps.

/**
 * Bird - Constructor and child class of Animal.
 * @param {Object} aType - Type of the animal
 * @param {Object} aName - Name of the animal 
 */

function Bird(aType, aName, bType) {
 //Calling super class constructor
 Animal.call(this, aType, aName);
 this.birdType = bType;
}
//Setup the protype chain
Bird.prototype = new Animal();
//Set the constructor attribute to Bird
Bird.prototype.constructor = Bird;

//Add methods to Bird class.
Bird.prototype.fly = function() {
 console.log('I am flying');
};

Let's create an object for Bird and try to access methods and properties of the parent class.

bird = new Bird('Ducks', 'Tundra Swan', 'Ducks');
bird.eat(); //Calling super class method
bird.fly(); //Calling child class method

Final result of the application is given below.


All modern library like jQuery and Ext JS has provided simple utility function for extending other class something like that.

var Pointer = Pointer || {};
/**
 * Simple utility function for extend.
 */
Pointer.extend = function(subClass, superClass) {
 var F = function() {};
 F.prototype = superClass.prototype;
 subClass.prototype = new F(); 
 subClass.prototype.constructor = subClass;
 
 subClass.superclass = superClass.prototype;
 /**
  * The below condition is important for handling
  * extend(Person, Object); //Person - Sample class extending JavaScript Object class.
  */
 if(superClass.prototype.constructor === Object.prototype.constructor) {
  superClass.prototype.constructor = superClass;
 }
};

Simple another example using above extend function.
/**
 * This is parent class constructor
 * @param {Object} name
 */
var Person = function(name) {
 Person.superclass.constructor.call(this, name);
 this.name = name;
};

Pointer.extend(Person, Object); //Person is extending Object class.

Person.prototype.getName = function() {
 return this.name;
};

/**
 * This is child class of the Person.
 * 
 * @param {Object} name
 * @param {Object} profession
 */
var Author = function(name, profession) {
 //Person.call(this, name);
 Author.superclass.constructor.call(this, name);
 this.profession = profession;
};

Pointer.extend(Author, Person);

Author.prototype.getProfession = function() {
 return this.profession;
};

For testing, let's create some objects.

person1 = new Author('Suriya', 'Solution Developer');
person2 = new Person('Hilar');

The result of the above example is should be something like this.


Comments