I have two 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 am myFunc");
}
}
});
return x;
}
var y=new Obj1("Your Name");
var z=new Obj2("Her Name");
y.getName();
z.getName();//works fine in this case
when I call z.getName()
while creating the Obj2
with the following constructor function it results in an 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 am myFunc");
}
}
});
//return x;
}
I get the same error when I try to do it this way
function Obj2(name){
var x=new Obj(name);
x.prototype={};
x.prototype=Object.defineProperties(x.prototype,{
myFunc:{
value:function(){
alert("I am myFunc");
}
}
});
return x;
}
what's going on I am in complete confusion why don't the second and third ways of writing constructor for creating Obj2
inherit the method getName()
,Is the first way of constructor inheriting or creating another copy of x
with newly defined properties on x.prototype
?