prototypal inheritance: the prototype object

J

Jessica

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
 
V

VK

If an object cannot find the requested method in itself then it looks
for it in its prototype chain. If it has the requested method in
itself then it doesn't look for it anymore any deeper even if it has a
method with the same name in its prototype chain (first match rule, or
shadowing). Each object instance gets pre-made prototype chain from
the instance constructor. What this prototype chain will look like
depends on what did you do with it on the construction stage.

Please ask if any more questions.
 
J

Jessica

I know about the prototype chain. But my question was about the
prototype object
that was created as you can see in my example.

I ask in another way:

if you create 10 instances of B, how many prototype objects (created
like this: B.prototype = new A; ) are existing in memory?
One for all instances? Or 10?

Greetings Jess
 
V

VK

if you create 10 instances of B, how many prototype objects (created
like this: B.prototype = new A; ) are existing in memory?
One for all instances? Or 10?

As it was explained before: just one for all 10 instances. But for
each instance you can shadow the prototyped property/method by its own
after the creation.

P.S. btw B.prototype.constructor = B; in your OP has nothing to do
with the prototype inheretance and it doesn't affect on anything.
 
J

Jessica

Thanx for your answer.

I did the re-set of the constructor property to be able to ask for
this property later in that code...
(For example to make new B-instances or augment all instances
prototypically.
 
T

Thomas 'PointedEars' Lahn

Jessica said:
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;

I thought so once, but it turned out that this approach (taken from the
Netscape JavaScript 1.x Guide) is _not_ the proper way to append to the
prototype chain. The above will create the following prototype chain:

new B() --> B.prototype === new A() --> A.prototype --> Object.prototype

Meaning that any properties that A objects do not inherit directly from
A.prototype, as they created in the local context of A, will be set for
B.prototype as well. And since all B objects would inherit from the same
A object, they would share not only its properties but also the property values.

However, for proper inheritance B.prototype should inherit new properties
only from A.prototype, and separate B objects should not share their
property values implicitly. Therefore, you are looking for this frequently
posted solution:

function inheritFrom(o)
{
function Foo() {}
Foo.prototype = o;
return new Foo();
}

function B()
{
A.call(this, ...);
// ...
}

B.prototype = inheritFrom(A.prototype);

This will set up the following prototype chain:

new B() --> B.prototype === new Foo() --> A.prototype --> Object.prototype

See
http://groups.google.com/group/comp.lang.javascript/msg/8a59bba12a022e1a?dmode=source
The line "B.prototype = new A()" creates an object in the memory for
example at the adress "x1".

You should not bother with addresses in a language that uses object
references, and not, or only at the lowest level of the implementation,
explicit pointers to memory addresses where data is stored.
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":

You should refrain from using class-based terminology with prototype-based
inheritance. The new object merely had B as its constructor, or one could
say (as I do) it is a B object. To say it is "an instance of B" would imply
a kind of strict binding between those two objects that does not exist.
Don't let operators borrowed from Java, like `instanceof', confuse you.
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,

In your case, as the prototype chain was not set up correctly, yes.
or does every instance have an own prototype-object (a copy?) that
contains the value?

Iff the prototype chain was set up properly, all objects created with the
same constructor share the properties (but not necessarily the property
values) of the corresponding prototype object.


HTH

PointedEars
 
J

John G Harris

Therefore, you are looking for this frequently
posted solution:

function inheritFrom(o)
{
function Foo() {}
Foo.prototype = o;
return new Foo();
}
<snip>

Now you need to add some methods to the returned object. Obviously, you
encapsulate all this in a function. Then, when you copy and paste this
to another project there is less chance of forgetting to copy some of
the code.

Ooh, look! A constructor function. Now inheritFrom is no longer needed.
(It was only there to make javascript look like another language
anyway).

John
 
J

John G Harris

Thanx for your answer.

I did the re-set of the constructor property to be able to ask for
this property later in that code...
(For example to make new B-instances or augment all instances
prototypically.

Do you really think you'll need to do that very often? You're saying you
have an object, you don't know what kind it is, but you want to make
another one of the same kind. Somehow you do know what parameters are
needed and what you can do with the new object later on. I think this is
quite unusual, especially for the kind of programs written in
javascript. Doing new B() is simpler and easier to proof-read.

Even more so for messing about with an unknown prototype chain!

John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,782
Messages
2,569,616
Members
45,297
Latest member
EngineerD

Latest Threads

Top