I'm trying to use prototypal inheritance, but I'm having trouble.
This doesn't work
var Parent = function(){
}
var Child = function(){
this.__proto__ = new Parent();
}
var child = new Child();
console.log(child instanceof Child) //logs false
But this does
var Parent = function(){
}
var Child = function(){
}
Child.prototype = new Parent();
var child = new Child();
console.log(child instanceof Child) // logs true
The only reason why I want the first option is so that I can leverage the parent's constructor. I'm guessing this is the problem, but I'm not that great at javascript. How do I make this work?
this.__proto__ = new Parent()you're saying "Okay new object, stop being aChildand start being aParentinstead," so it's not surprising that the object is no longer and instance of Child.this? Why doesn't the second method do the same thing then? Is it because I am not in the scope ofnew Childwhen I set the prototype?this(instance members): stackoverflow.com/a/16063711/1641941