Inheritance in javascript with example
In the previous article I have discussed about IEnumerable and IEnumerator in C# , Useful shortcut keys of Visual studio,Delegates in C# with simple example,What is CDATA Tag ?,Ref and Out Keyword in C#,What is Abstract class in C#? Today i am going to tell you about Inheritance in javascript with example.
Inheritance in JavaScript
Let's start with inheritance that we see in c# or java in which a class inherits behavior of other class or in simple way we can say that creating a class by using other class behavior is inheritance.
JavaScript does not support Inheritance as C# and Java. Ohhh!! really then what this topic about?? Yes you have heard right,JavaScript does not support traditional inheritance like C# and java does. JavaScript supports prototypical inheritance.
As JavaScript is all about object.It does not support class structure like c# and java so how inheritance take place in JavaScript. In JavaScript one object can inherits other object by using prototype.First we understands the prototype property in JavaScript.
Below example will show how to create a prototype property for JavaScript method -
In above example we are adding a print method to ShowName() method using prototype property.To do inheritance using prototype property ,let take same example and create a new object using ShowName() method.
Above example is the simplest example to understand the concept of inheritance in JavaScript using prototype property.
Prototype in JavaScript
Every JavaScript function has a property called prototype. This property is empty by default, and you can attach as much as properties and methods on this prototype property.To implement inheritance in JavaScript this prototype property is used.Inheritance in javascript : Every JavaScript function has a property called prototype. This property is empty by default, and you can attach as much as properties and methods on this prototype property.To implement inheritance in JavaScript this prototype property is used.
Below example will show how to create a prototype property for JavaScript method -
In above example we are adding a print method to ShowName() method using prototype property.To do inheritance using prototype property ,let take same example and create a new object using ShowName() method.
Above example is the simplest example to understand the concept of inheritance in JavaScript using prototype property.
Why is Prototype Important and When is it Used?
Prototype is very important property in JavaScript because as I told earlier in this post that JavaScript does not support have classical inheritance based on Classes as C#,Java and other object oriented language supports,
and therefore all inheritance in JavaScript is only possible through the prototype property.
To understand it better below is an another simple demonstration of inheritance in JavaScript -
I hope you have enjoyed this tutorial of Inheritance in javascript. I would like to have feedback from you. Your feedback, question ,queries or comments are always welcome.
Thanks
Thanks
Inheritance in javascript with example
Reviewed by CodiBucket
on
01:41
Rating:
Java Script is Object based language how is it possible
ReplyDeleteHi Subash sunku ,your question is 100% valid. As i mentioned in post that JavaScript does not support inheritance like c# ,java or any other object oriented language does.JavaScript supports prototypical inheritance,means you can achieve inheritance in JavaScript by using prototype property.Please go through the example mentioned in post, you will understand the inheritance in JavaScript.
DeleteThat's the beauty of JavaScript. Let me know if you have any question.
How
ReplyDeleteJavaScript has prototype property which is used to achieve inheritance in JavaScript.
DeleteJavaScript has prototype property which is used to achieve inheritance in JavaScript.
ReplyDelete👍👍👍
ReplyDeleteGood article for beginners.
ReplyDelete