Inheritance in JavaScript through prototype
// http://javascriptissexy.com/javascript-prototype-in-plain-detailed-language/
function Plant(){
this.country = "India";
this.isOrganic = true;
}
// Add the showNameAndColor method to the Plant prototype property
Plant.prototype.showNameAndColor = function(){
console.log("Name : " + this.name + " Color: " + this.color);
}
// Add the amIOrganic method to the Plant prototype propert
Plant.prototype.amIOrganic = function(){
if(this.isOrganic)
console.log("I am orgainc, Baby!");
}
function Fruit(fruitName, fruitColor){
this.name = fruitName;
this.color = fruitColor;
}
// Set the Fruit's prototype to Plant's constructor, thus inheriting all of Plant.prototype methods and properties
Fruit.prototype = new Plant();
// Creates a new object, aBanana, with the Fruit constructor
var aBanana = new Fruit("Banana", "Yellow");
// Here, aBanana uses the name property from the aBanana object prototype, which is Fruit.prototype
console.log(aBanana.name);
// Uses the showNameAndColor method from the Fruit object prototype, which is Plant.prototype. The aBanana object inherits all the properties and methods from both the Plant and Fruit functions
aBanana.showNameAndColor();
aBanana.amIOrganic();