prototypal inheritance: the prototype object

Giganews Newsgroups
Subject: prototypal inheritance: the prototype object
Posted by:  Jessica (JessAlba…@googlemail.com)
Date: Fri, 30 Nov 2007

Hi,

I have a question referring prototypal inheritance in javascript.
My example:

function A() {};
A.prototype = {
    count: 10,
    doit : function () {alert ("doit");}
};

function B() {};

B.prototype = new A;
B.prototype.constructor = B;

The line "B.prototype = new A()" creates an object in the memory for
example at the adress "x1".
The B-definition references this object in its prototype-property.
Then I create an instance of B. I try to get the value of "count":

var test = new B();
var tmp = test.count;

Is it still the object at the adress "x1" that gives me the values of
the property, or does every instance have an own prototype-object (a
copy?) that contains the value?

Thank you very much in advance.

Greetings Jess

Replies