Richard Trahan <rt*****@optonline.net> writes:
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:
<URL:http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/obj2.html#1013803>
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
--
Lasse Reichstein Nielsen -
lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'