Are There Problems With Replacing A Javascript Constructor's .prototype Rather Than Adding To It?
I've come across another developer's code which does something like this to define a Javascript type: function Ninja() {} Ninja.prototype = { swingSword: function(){ return t
Solution 1:
When you "inherit" from another prototype you usually replace the entire prototype, if constructor is important you can re set it to the "correct" value:
varChild = function(){};
//"inherit" from ParentChild.prototype=Object.create(Parent.prototype);
//reset the constructorChild.prototype.constructor = Child;
So in your case just repair the constructor after you set the prototype:
varNinja = function() {}
Ninja.prototype = {
swingSword: function(){
returntrue;
}
};
// you can put the next line anywhere you want to fix the problem// http://stackoverflow.com/a/16063711/1641941 under this.constructorNinja.prototype.constructor=Ninja;
Or:
varNinja = function() {}
Ninja.prototype = {
swingSword: function(){
returntrue;
},
constructor:Ninja
};
Post a Comment for "Are There Problems With Replacing A Javascript Constructor's .prototype Rather Than Adding To It?"