object inheritance question

R

Richard Trahan

In the 1.5 Guide, Chapter 8 (Details of the Object Model), last
sentence: "The dennis object does not inherit this new property".

I don't understand why this is so. A few pages earlier, it says "If you
want to change the value of an object property at run time and have the
new value be inherited by all descendants of the object, you cannot
define the property in the object's contructor function. Instead, you
add it to the constructor's associated prototype." This is followed by
an example.

The 'dennis' example is different in that the property added to the
prototype is not a changed value, but a new property. I understand that
new properties can be added at runtime simply by adding them to the
constructor object, but the rules aren't clear regarding how the system
treats adding a brand new property to the constructor object's prototype
property at run time.

Can anyone clarify this for me, please.
 
L

Lasse Reichstein Nielsen

Richard Trahan said:
In the 1.5 Guide, Chapter 8 (Details of the Object Model), last
sentence: "The dennis object does not inherit this new property".

That would be this page:
I don't understand why this is so. A few pages earlier, it says "If
you want to change the value of an object property at run time and
have the new value be inherited by all descendants of the object, you
cannot define the property in the object's contructor
function. Instead, you add it to the constructor's associated
prototype."

I am not entirely sure what they mean by "descendants" here, but if I
understand the meaning correctly, what they say is:

If you want to be able to change a property for all objects created
by a constructor (by setting the value on the prototype), don't
assign that property in the constructor (because that would shadow
the property of the prototype in all objects).
The 'dennis' example is different in that the property added to the
prototype is not a changed value, but a new property.

It is also different in that the property is added to the prototype
of "Hobbyist", but the prototype of "Hobbyist" is not in the prototype
chain of an object created from "Engineer". The prototype chain of
"Engineer" objects starts with Engineer.prototype, which is an instance
of "WorkerBee", not "Hobbyist".

That is why adding a property to "Hobbyist.prototype" does not affect
"Engineer" objects. "Engineer.prototype" simply doesn't inherit from
"Hobbyist.prototype".
I understand that new properties can be added at runtime simply by
adding them to the constructor object, but the rules aren't clear
regarding how the system treats adding a brand new property to the
constructor object's prototype property at run time.

It doesn't matter that it is new. Every time you look up a property,
the same steps are followed:
1. Check whether the object has the property itself.
2. For each object on the prototype chain, check whether that object
has the property.
3. If none of the above, the result is "undefined".

In the example of "dennis", he is created using "Engineer". That means
that the prototype chain of "dennis" starts with "Engineer.prototype".
That has been assigned like this:
Engineer.prototype = new WorkerBee;
That means that the next object in the chain is "WorkerBee.prototype"
From above we find:
WorkerBee.prototype = new Employee;
So, the next element is "Employee.prototype". Since that has not been
assigned to, it's just some object (with "Object.prototype" as its
prototype).

The entire property resolution chain of "dennis" is:
dennis
Engineer.prototype
WorkerBee.prototype
Employee.prototype
Object.prototype
So, assigning a property to "Hobbyist.prototype" doesn't change anything
for "dennis". Assigning to a property of "Workerbee.prototype", whether
it exists already or not, will change "dennis", unless "Engineer.prototype"
or "dennis" also has that property.


/L
 
R

Richard Trahan

It is also different in that the property is added to the prototype
of "Hobbyist", but the prototype of "Hobbyist" is not in the prototype
chain of an object created from "Engineer".

Thank you! That is the part I overlooked; new or changed properties
"trickle down" (or up, depending on your point of view) only through the
prototype chain, and the Hobbyist object is not in it. All other parts
of this I understand.

BTW, their use of "descendants" comes after a disclaimer in which they
beg indulgence to use class-oriented nomenclature, which is not strictly
applicable to js.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top