Javascript prototype is easy
- Javascript prototype is quite fuzzy word for javascript developers in the beginning.
- Prototype is the property which is available for all javascript functions by default.
- This property helps to attain inheritance in javascript.
- As most of them aware of classical inheritance concept i.e inherits the methods and properties from parent class.
- But, In javascript there is no class keyword to achieve inheritance.
- Javascript is function based object orient programming language.
Find the following simple snippet to understand prototype.
function abc() {
}
Prototype methods and property created for function abc
abc.prototype.testProperty = 'Hi, I am prototype property';
abc.prototype.testMethod = function() {
alert('Hi i am prototype method')
}
Creating new instances for function abc
var objx = new abc();
console.log(objx.testProperty); // will display Hi, I am prototype property
objx.testMethod();// alert Hi i am prototype method
var objy = new abc();
console.log(objy.testProperty); //will display Hi, I am prototype property
objy.testProperty = Hi, I am over-ridden prototype property
console.log(objy.testProperty); //will display Hi, I am over-ridden prototype property
Same way prototype methods can be overridden in the instance
Note: If you are still not able to understand please comment your query.
Leave a Comment