By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,704 Members | 1,475 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,704 IT Pros & Developers. It's quick & easy.

prototypal inheritance: the prototype object

P: n/a
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
Nov 30 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.
Nov 30 '07 #2

P: n/a
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
Nov 30 '07 #3

P: n/a
VK
On Nov 30, 9:26 pm, Jessica <JessAlb...@googlemail.comwrote:
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.

Nov 30 '07 #4

P: n/a
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.

Nov 30 '07 #5

P: n/a
VK
On Nov 30, 10:55 pm, Jessica <JessAlb...@googlemail.comwrote:
Thanx for your answer.
You are welcome
I did the re-set of the constructor property to be able to ask for
this property later in that code...
Before you may want to read "Prototypes and constructors" by Eric
Lippert at
http://blogs.msdn.com/ericlippert/ar.../06/53352.aspx

That covers the bases pretty well IMO
Nov 30 '07 #6

P: n/a
On Fri, 30 Nov 2007 at 21:20:13, in comp.lang.javascript, Thomas
'PointedEars' Lahn wrote:

<snip>
>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
--
John Harris
Dec 2 '07 #7

P: n/a
On Fri, 30 Nov 2007 at 11:55:44, in comp.lang.javascript, Jessica wrote:
>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
--
John Harris
Dec 2 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.