Skip to content

Instantly share code, notes, and snippets.

@muralikrishnat
Created September 18, 2015 13:05
Show Gist options
  • Save muralikrishnat/45ebd0eef58570117a7d to your computer and use it in GitHub Desktop.
Save muralikrishnat/45ebd0eef58570117a7d to your computer and use it in GitHub Desktop.
Inheritance in Javascript with simple example
//Intro : Basic explanation of Javascript Inheritance concept
//there are many ways to achieve inheritance feature in Javascript
//this is one of the approach I follow most of the time.
//Declaring Parent Class/Function
//with three properties and one method
var ParentClass = function(property1, property2){
this.Property1 = property1;
this.Property2 = property2;
this.Property3 = null;
this.method1 = function(method1arg1){
this.Property3 = method1arg1 + ' Appending in Parent';
};
};
//Declaring Child Class
var ChildClass1 = function(property1, property2, property3){
//Getting ParentClass properties and methods into Child context
ParentClass.call(this, property1, property2);
this.method1 = function(method2arg1){
this.Property3 = method2arg1 + 'Appending in Child';
};
};
var ChildClass2 = function(property1, property2, property3){
//Getting ParentClass properties and methods into Child context
ParentClass.call(this, property1, property2);
//preserving the parent method into one of the child method
//that can be used as parent method in further
this.ParentMethod1 = this.method1;
//child class method that can invoke parent method after doing some stuff in child class
this.method1 = function(method2arg1){
//calling patent method that preserved while creating of object
//do some child stufff
this.ParentMethod1.call(this,method2arg1);
};
};
//creating object for child class 1 which overwrties the parent method
var child1object = new ChildClass1('Hansel', 'Hunter', 23);
//creating object for child class 2 which invokes the parent method on call of child method
var child2object = new ChildClass2('Gretel', 'Hunter', 23);
console.log(child1object.Property3);
child1object.method1('child1 ');
//prints 'child1 Appending in Child' calling 'method1' will invoke child method
console.log(child1object.Property3);
console.log(child2object.Property3);
child2object.method1('child 2');
//prints 'child1 Appending in Parent' calling 'method1' will invoke child method
console.log(child2object.Property3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment