Misunderstanding I have about javascript prototype inheritance

Posted by Ilya on Stack Overflow See other posts from Stack Overflow or by Ilya
Published on 2010-12-22T05:32:35Z Indexed on 2010/12/22 7:54 UTC
Read the original article Hit count: 512

Filed under:
|

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

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about inheritance