Javascript inheritance

Inheritance in javascript can be implemented easily using the concept of mixin. Below is an exmple to demonstrate javascript inheritance:

ClassA = function(){
	this.display = function(){
		console.log("here's a display function in ClassA");
	}

	this.str = function(){
		return "string in ClassA";
	}
}

ClassB = function(){
	ClassA.call(this);

//overriden method
	this.str = function(){
		return "string in ClassB";
        }
}

//instantiate object of ClassB
obj = new ClassB();

//calling display method from ClassA
obj.display();

//this will invoke str method in ClassB NOT in ClassA
console.log(obj.str());
here’s a display function in ClassA
string in ClassB string in ClassB
Leave a comment

0 Comments.

Leave a Reply

( Ctrl + Enter )