W
wilq
I'm struggling with this problem for some time, and looking for clear
answer... Why this is commonly adviced to set
myObject.prototype.constructor back to myObject in any prototypical
coding in JS? I heard some version with constructor being wiped out
(sic!) or instanceof not working correctly, but all mine test shows
that its completly fine. Is this usefull in any inheritance pattern? I
just find out that this could be helpful if you test inside function
things like:
this.constructor == myObject
but usually you would do
this instanceof myObject
that would work even without setting .prototype.constructor back to
myObject...
So... can anyone help me please? Some examples or links to post would
be really great! I already look on this group, but I never saw a topic
strictly about this subject so I hope you don't get bad if this was
already discussed before...
Here is a code that based on I did some test:
var Parent = function(){
alert("parent constructor");
}
Parent.prototype.parentFunc = function(){
alert("I'm a parrent");
}
var Child = function(){
alert("child constructor");
}
Child.prototype = {
childFunc : function(){
alert("I'm a Child");
}
}
//Child.prototype.constructor = Child;
var a = new Child();
alert(a instanceof Child);
alert(a instanceof Parent);
answer... Why this is commonly adviced to set
myObject.prototype.constructor back to myObject in any prototypical
coding in JS? I heard some version with constructor being wiped out
(sic!) or instanceof not working correctly, but all mine test shows
that its completly fine. Is this usefull in any inheritance pattern? I
just find out that this could be helpful if you test inside function
things like:
this.constructor == myObject
but usually you would do
this instanceof myObject
that would work even without setting .prototype.constructor back to
myObject...
So... can anyone help me please? Some examples or links to post would
be really great! I already look on this group, but I never saw a topic
strictly about this subject so I hope you don't get bad if this was
already discussed before...
Here is a code that based on I did some test:
var Parent = function(){
alert("parent constructor");
}
Parent.prototype.parentFunc = function(){
alert("I'm a parrent");
}
var Child = function(){
alert("child constructor");
}
Child.prototype = {
childFunc : function(){
alert("I'm a Child");
}
}
//Child.prototype.constructor = Child;
var a = new Child();
alert(a instanceof Child);
alert(a instanceof Parent);