javascript - Is it inheritance?Could some body explain? -


i have 2 objects

function obj1(name){    this.prototype={};    this.prototype.name=name;    this.prototype.getname=function(){         alert(this.name);     }; } function obj2(name){    var x=new obj1(name);    x.prototype=object.defineproperties(x,{    myfunc:{        value:function(){        alert("i myfunc");        }    }    });    return x; } var y=new obj1("your name"); var z=new obj2("her name"); y.getname(); z.getname();//works fine in case 

when call z.getname() while creating obj2 following constructor function results in error saying "z has no method getname()"

function obj2(name){    //var x=new obj(name);    this.prototype=new obj(name);    this.prototype=object.defineproperties(this.prototype,{    myfunc:{        value:function(){        alert("i myfunc");        }    }    });    //return x; } 

i same error when try way

    function obj2(name){    var x=new obj(name);    x.prototype={};    x.prototype=object.defineproperties(x.prototype,{    myfunc:{        value:function(){        alert("i myfunc");        }    }    });    return x; } 

what's going on in complete confusion why don't second , third ways of writing constructor creating obj2 inherit method getname(),is first way of constructor inheriting or creating copy of x newly defined properties on x.prototype?


Comments