473,396 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

prototypal inheritance: the prototype object

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
7 1368
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Robert Spoons | last post by:
Can you look over this code, preferably try it, and comment? I believe the 'extend' function below will allow you to use full 'class inheritance' in javascript, but I would like to verify it. ...
2
by: stephane | last post by:
Hi all, What I am trying to achieve is an 'inherits' method similar to Douglas Crockford's (http://www.crockford.com/javascript/inheritance.html) but that can enable access to the superclass'...
2
by: Kevin Newman | last post by:
I have been playing around with a couple of ways to add inheritance to a JavaScript singleton pattern. As far as I'm aware, using an anonymous constructor to create a singleton does not allow any...
31
by: John W. Kennedy | last post by:
I quite understand about prototypes and not having classes as such, but I happen to have a problem involving blatant is-a relationships, such that inheritance is the bloody obvious way to go. I can...
6
by: mmcloughlin | last post by:
I'm learning about objects and am trying to figure out how basic inheritance works. I've got into the habit of explicitly setting the prototype object with an object literal as it seems to make the...
36
by: Pacific Fox | last post by:
Hi all, haven't posted to this group before, but got an issue I can't work out... and hoping to get some help here ;-) I've got a base object that works fine with named arguments when called...
6
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
3
by: linc | last post by:
I am planning on writing a fairly large system in javascript and would like some advice on using the Douglas Crockford 'beget' function to implement prototypal inheritance. Searching the internet...
6
by: howa | last post by:
Consider example: Animal = function(age) { this.age = age; }; Animal.prototype.sleep = function() { alert("Animal Sleeping..."); };
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.