Misunderstanding I have about javascript prototype inheritance
- by Ilya
Simple questions.
function p()
{
function A()
{
this.random = "random";
}
A.prototype.newfunc = function(){ alert("5");}
function B()
{
}
B.prototype = new A();
var bObj = new B();
}
Q1: When I set B's prototype, I don't get how B's prototype property will update when/if A's prototype is updated. I mean, to me it just inherits/copies all those properties. It's not like it's:
B.prototype = A.prototype
where B and A are one in the same.
Q2: After A is being returned and intialized to the prototype object of B, how does JS know not to include that prototype property? What I mean is, we never have this type of situation occuring as the JS interpreter knows just to chop off the property of A's prototype:
B.prototype = new A(); //any A object has an associated prototype object
B.prototype.prototype;//after initialization we no longer have the separate prototype property of A