Subclassing, Inheritance, RE subclass "constructor" and inherited members

V

vbgunz

// Referring to JavaScript the Definitive Guide: 9.5 through 9.5.2

// the base and sub classes are perfect. the following code
// is an example case, regarding WHY delete base members from
// a subclass AND what is wrong with the loop ONLY
// returning -> prototype :/

// Ex 01:
print(subInstance.hasOwnProperty("baseMember")); // false
for (var p in SubClass){
print(p);
} // prototype


// Ex 02:
delete SubClass.prototype.baseMember; // not customizing, delete?
print(subInstance.hasOwnProperty("baseMember")); // false
for (var p in SubClass){
print(p);
} // prototype

// Why delete the members in subclass if I inherit them from
// baseclass? is it the same reason you might want to put constants
// & methods in the prototype? Is it to avoid duplication and memory
// hogging? e.g., I don't actually need to override the base members
// so should I really delete them from subclass?

// Why iterating over the properties in the subclass yield ONLY ->
// prototype? shouldn't I at least get all properties back defined in
// ALL classes (Ex 01) AND after deleting those inherited from base on
// sub, shouldn't I at least get those properties of sub (Ex 02)? Both
// Examples, have several members!
 
T

Thomas 'PointedEars' Lahn

vbgunz said:
// Referring to JavaScript the Definitive Guide: 9.5 through 9.5.2

Argh! Shall we not remove that recommendation from the FAQ already?
// the base and sub classes are perfect. the following code

There are no classes. You have been lied to. Unfortunately, I know of no
book about the language that does not lie to you, so you better stick to
this newsgroup instead.
// is an example case, regarding WHY delete base members from
// a subclass AND what is wrong with the loop ONLY
// returning -> prototype :/

// Ex 01:
print(subInstance.hasOwnProperty("baseMember")); // false

There are no instances either. There are only objects, which inherit
from other (prototype) objects. The programming language you are learning
is an object-oriented language that uses prototype-based inheritance.
for (var p in SubClass){
print(p);
} // prototype


// Ex 02:
delete SubClass.prototype.baseMember; // not customizing, delete?
print(subInstance.hasOwnProperty("baseMember")); // false
for (var p in SubClass){
print(p);
} // prototype

// Why delete the members in subclass if I inherit them from
// baseclass?

Probably to show that the object referred to by `subInstance' does
not have a property named `baseMember' that is not inherited from
`SubClass.prototype'. Because that is what subInstance.hasOwnProperty(),
which is inherited from Object.prototype through the prototype chain,
tests for.
is it the same reason you might want to put constants
// & methods in the prototype?

Parse error.
Is it to avoid duplication and memory
// hogging?

Probably it is to demonstrate that some properties are inherited from
prototype objects and some are not.
e.g., I don't actually need to override the base members
// so should I really delete them from subclass?
No.

// Why iterating over the properties in the subclass yield ONLY ->
// prototype?

Because that is the only enumerable property of that object.
shouldn't I at least get all properties back defined in
// ALL classes (Ex 01)

As there are no classes: yes, you should not.
AND after deleting those inherited from base on
// sub, shouldn't I at least get those properties of sub (Ex 02)?

No, you only get enumerable properties.
Both // Examples, have several members!

So? Those are merely properties, BTW. Class-based terminology does not
apply to implementations of ECMAScript Edition 3 and below, such as
client-side J(ava)Script.

There is no need to write your questions as (single-line) script comments
(single-line comments will break anyway given enough quotation levels.)
I am quite sure your readers are capable of distinguishing script code and
your prose. That said, it is not a bad idea to indent script code a bit
(I usually use two leading spaces) in order to avoid confusion.


HTH

PointedEars
 
T

Thomas 'PointedEars' Lahn

FAQEditor said:
Thomas 'PointedEars' Lahn said the following on 8/28/2007 5:27 PM:

If you give me a better viable alternative I will be glad to change it.

Given that there are most certainly no good books, that nobody who deems
himself an expert of the language will review a book unless paid for that,
and (from the examples posted so far) most certainly what is currently
recommended falls into the category of bad books, I think it would be
acceptable to state that fact instead. No recommendation for a book is
better than a bad recommendation.

With the bad recommendation then removed, it would be a Good Thing if the
FAQ recommended against certain books, providing evidence why they can be
considered bad, using examples from postings or personal experience. We
could easily filter out the bad books that way (dismiss them right away if
author, title and edition fits), and have at least a realistic chance to see
someone quoting from a good book without having to review all books that are
on the market and are yet to come.


PointedEars
 
V

vbgunz

David Flanigan doesn't state JavaScript is a class-based inherited
language. He does explicitly state that it is a prototype-based
inherited language. I am sorry if I caused any confusion. I only
referenced the part of the book I did because I was aiming to cover
that particular topic. The analogies of inheritance. I admit my poor
examples were not his and probably served no good at what I was after :
(

Please, take the following example:

// Ex 01
function Bon(x){
this.x = x;
}

Son.prototype = new Bon();
function Son(y, x){
Bon.call(this, x);
this.y = y;
}

var c = new Son(10, 20);
print("x is", c.x, "and y is", c.y);


Now, this is probably the best way I can explain what I am trying to
figure out. David states I must do at least the following on every
inheriting constructor I make:

// Ex 02
delete Son.prototype.x;
Son.prototype.constructor = Son;


without much explanation on why I am too delete what I inherit I've
come to the conclusion that it is probably due to the same reasons I
would put constants and methods in the prototype (to avoid
duplication). I feel I may be correct *but* if I am, I find it too
cumbersome to have to do that every time. I just feel there has to be
a better way or at least great reason this will have to be done.

Also, the constructor reassignment back to "Son" dirties any
iterations I make e.g., without reassigning constructor, I get "x, y"
but with it I get "x, y, constructor". I am not the brightest guy ever
but don't think I want "constructor" coming back at all. But a case in
point as to why I might want to do it is the following:

// Ex 03
// before reassigning constructor
print(c.constructor == Son); // -> false
print(c.constructor == Bon); // -> true

// after reassigning constructor
print(c.constructor == Son); // -> true
print(c.constructor == Bon); // -> false


I will continue reading the book and knowing my luck, he will answer
all of my questions in the next few pages. if so I apologize but he
had a really big example and I was determined to figure it out. he
does comment it but I just don't get the reasons behind it, except to
just do it. I apologize for any waste of time. I am just trying my
best to learn here.

Thank you!
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>,
The last time that section came up it was pointed out that if the FAQ
doesn't list a book then people will post a kazillion times asking
"What book....." because there isn't one in the FAQ. That said, I do
agree with the assertion that the entry should be expanded to explain
that it isn't a "flawless" book but that it is the "least flawless" of
the books available.


So : you are the FAQ maintainer, and you should therefore expand it.

If you can bring yourself to update the published version of the FAQ
whenever you have new material, rather than waiting an indefinite time
to produce a highly-polished version, then there's no problem with the
content. Just make a significant change to the section (preferably just
before Bart posts it), and with "Altered YYYY-MM-DD", and, as you
yourself have recognised, you'll soon be told if you've got it wrong --
and in enough detail for the next version to be nearly right.

IMHO, there's not much point in recommending an out-of-print book.

However, O'Reilly are being somewhat despicable in asking £35.50 for a
book they sell at $49.99 - Bloomberg puts the pound at 2.021 USD at the
moment, so £24.75 would be about right. And my IE6 won't show the ToC
happily.

FAQ :
Delete the first two lines, and the 4th Edn links.
Move the reference to the Pocket book to the bottom of 3.1.
Now move the blog bit to the bottom.
Change paragraph "The Fifth Edition ..." to "One of the best books is -"
Change "By" to "by".
Correct the spelling.
Adjust the layout slightly to use fewer non-empty lines.
 
P

Peter Michaux

Thomas 'PointedEars' Lahn said the following on 8/28/2007 5:27 PM:



If you give me a better viable alternative I will be glad to change it.

There have been quite a few JavaScript books out in the last year. I
haven't seen them all but most are not complete-ish references like
Flanagan's book and so wouldn't seem appropriate as a single
recommendation. I think Flanagan's book is the best I've seen and it
has been and remains very useful to me as a first reference. Imagine
trying to perfectly write a book on JavaScript and all the browser
bugs and undocumented features. Flanagan has done a difficult,
remarkable and admirable job.

If the faq is going to be expanded for this I think it might be
beneficial to say something like "Flanagan does a lot of Java
programming and written books about it and the Java programming style
has influenced his style of JavaScript programming." I did also
suggest a comment about the fact that there is quite a bit of errata.
I'm glad the link to errata is there but a comment emphasizing its
importance might be good. Time to ditch the fourth edition from the
faq. The Fifth edition is just as good (but unfortunately the index
isn't as complete.) His name is not spelled "Davaid".

Peter
 
D

Dr J R Stockton

In comp.lang.javascript message said:
Dr J R Stockton said the following on 8/30/2007 5:13 PM:

What is it about plain English that you can not comprehend? I won't
waste the time to try to explain it again.

You should spend your time in producing and publishing up-to-date FAQ
releases. You would then not be tempted to waste your time in self-
exculpatory posts.

I cannot understand any honourable explanation for your taking on the
maintenance of the group FAQ when you clearly have no intention of doing
the job in the customary and effective manner.

You should resign, or be deposed.

You could stand for re-appointment by the group, but we now know how
useless you are.
 

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
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top