prototypical inheritance... We have used many different types of inhertance which are based on classes but today we are going to discuss the prototypical inheritance in javascript. It is based on objects, you can simply inherit one object to other object.
Suppose we have two objects
var countries = {'India':1,'America':2};
var states = {'Delhi':1,'Washington':2};
lets inherit the states object to countries object with the help of special property _proto_
states.__proto__ = countries;
When you access the states then interpreter will find it in countries object. Please have a look an another example.
var animal = {
eat: function() {
alert( "I'm ok" )
this.full = true
}
}
var rabbit = {
jump: function() { /* something */ }
}
rabbit.__;proto__ = animal
rabbit.eat();
0 Comment(s)