Inheritance of prototype

by rishul matta

JavaScript

function Dad(){
    this.name="i am dad";
}

function Son(){
    this.nameOfChild="child";
}

//Son.prototype= new Dad();
//Son.prototype= new Dad();

var F= function(){}; // a temp F created which points to parent ka prototype
F.prototype=Dad.prototype;

Son.prototype= new F(); // new instance of F is created note that the name property of Dad wont be accessible as F.prototype points to Dad.prototype but name is not defined in Dad.Prototype

Son.prototype.constructor=Son;

Dad.prototype.newProp="i am added by dad's  prototype";

Son.prototype.childNewProp="i am added by son's prototype";

alert(Son.__proto__);
alert((new Son()).nameOfChild);
alert((new Son()).name); //invoking parent prop by child SHOULD BE UNDEFINED
alert((new Son()).newProp); //invoking parent prototype pro by child MUST WORK
alert((new Dad()).childNewProp); //invoking child method by parent object SHOULD BE UNDEFINED