473,387 Members | 1,897 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,387 software developers and data experts.

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.

Jul 23 '05 #1
2 1161
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.'
Jul 23 '05 #2
>
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.

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Martin | last post by:
When was inheritance intruduced into object oriented programming? More generally, does anyone know or have any sources on when the different features were introduced into object oriented...
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
19
by: G.Ashok | last post by:
Hi All, What is your weightage of the 3 characteristics of Object-Oriented Programming i.e. Inheritance, Encapsulation and Polymorphism. for a total of 100% Please quote your values and let's...
14
by: Craig Buchanan | last post by:
If I have two custom vb.net classes, where 80% of the properties are alike and there is one method with a matching signature, can i cast between one and the other? do i need to have each class...
3
by: Robert Abi Saab | last post by:
Hi everyone. I just finished a course on PostgreSQL and I found out that PostgreSQL doesn't provide any object relational features (as claimed in the official documentation), except table...
15
by: mr.peteryu | last post by:
Hi, Can someone explain the idea behind casting to an interface? For example: -> I have an IInterface that contains a Read() method. -> I have an object "obj" that implements IInterface. ...
5
by: Derek | last post by:
I have an advanced question that I hope there is a possible answer for... Say that I have a list of base objects and I wish to allow them to be passed to various routines which may wish to...
73
by: Water Cooler v2 | last post by:
I am new to PHP, just one day new. But I am otherwise a seasoned programmer on many other languages like C, VB 6, C#, VB.NET, Win32 platform and have some tiny bit experience in MFC, C++, Python. ...
14
by: emailscotta | last post by:
Some of the object properties in the Dojo Toolkit are set to objects but they are using syntax like this: object.property = new function() { this.property = someValue; this.property =...
3
by: terence.parker | last post by:
I'm writing a bunch of classes (specifically, for parsing) but have a question about class inheritance. I understand that with textbook object orientation, a class extension/inheritance...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.