object inheritance question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard Trahan

    object inheritance question

    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.

  • Lasse Reichstein Nielsen

    #2
    Re: object inheritance question

    Richard Trahan <rtrahan@optonl ine.net> writes:
    [color=blue]
    > In the 1.5 Guide, Chapter 8 (Details of the Object Model), last
    > sentence: "The dennis object does not inherit this new property".[/color]

    That would be this page:
    <URL:http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/guide/obj2.html#10138 03>
    [color=blue]
    > 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."[/color]

    I am not entirely sure what they mean by "descendant s" 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).
    [color=blue]
    > The 'dennis' example is different in that the property added to the
    > prototype is not a changed value, but a new property.[/color]

    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.protot ype, which is an instance
    of "WorkerBee" , not "Hobbyist".

    That is why adding a property to "Hobbyist.proto type" does not affect
    "Engineer" objects. "Engineer.proto type" simply doesn't inherit from
    "Hobbyist.proto type".
    [color=blue]
    > 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.[/color]

    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.proto type".
    That has been assigned like this:
    Engineer.protot ype = new WorkerBee;
    That means that the next object in the chain is "WorkerBee.prot otype"
    From above we find:
    WorkerBee.proto type = new Employee;
    So, the next element is "Employee.proto type". Since that has not been
    assigned to, it's just some object (with "Object.prototy pe" as its
    prototype).

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


    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Richard Trahan

      #3
      Re: object inheritance question

      >[color=blue]
      > 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".[/color]

      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 "descendant s" comes after a disclaimer in which they
      beg indulgence to use class-oriented nomenclature, which is not strictly
      applicable to js.

      Comment

      Working...