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

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

Post by vbgun » Thu, 30 Aug 2007 05:37:28


// 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 loops ONLY returning -> prototype :/

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


// Ex 02:
delete SubClass.prototype.baseMember; // Don't need to customize,
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!
 
 
 

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

Post by vbgun » Thu, 30 Aug 2007 05:44:23

// 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!

 
 
 

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

Post by Thomas 'Po » Thu, 30 Aug 2007 06:27:51


Argh! Shall we not remove that recommendation from the FAQ already?


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.


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.


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.


Parse error.


Probably it is to demonstrate that some properties are inherited from
prototype objects and some are not.


No.


Because that is the only enumerable property of that object.


As there are no classes: yes, you should not.


No, you only get enumerable properties.


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
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
 
 
 

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

Post by FAQEdito » Thu, 30 Aug 2007 07:06:39

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.

--
Randy
comp.lang.javascript FAQ - http://www.yqcomputer.com/
FAQ Notes: http://www.yqcomputer.com/
ECMAScript Language Specification via FAQ2.6
 
 
 

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

Post by Thomas 'Po » Thu, 30 Aug 2007 07:31:59


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
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from < http://www.yqcomputer.com/ ;
 
 
 

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

Post by Randy Web » Thu, 30 Aug 2007 14:12:09

Thomas 'PointedEars' Lahn said the following on 8/28/2007 6:31 PM:



Agreed with regards to JS books and most books in general.


There are too many people on the web that will quickly "deem themselves"
experts on JS that will review a book. Doesn't make that review worth
much though. I don't think your sentence conveys the meaning you meant
though.


That I, personally, disagree with. 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.


How would you filter out books if no "javascript expert" will review the
book though? How would you have a "realistic chance" of seeing someone
quote from a "good book" that doesn't exist?

Posts made from this name are my own opinions and have no bearing
whatsoever with my role as FAQ Editor though. If the consensus of the
group is to change an entry I change it even if I personally disagree
with it.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://www.yqcomputer.com/
Javascript Best Practices - http://www.yqcomputer.com/
 
 
 

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

Post by vbgun » Thu, 30 Aug 2007 22:20:06

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!
 
 
 

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

Post by Dr J R Sto » Sat, 01 Sep 2007 06:13:33

In comp.lang.javascript message < XXXX@XXXXX.COM >,
Wed, 29 Aug 2007 01:12:09, Randy Webb < XXXX@XXXXX.COM > posted:



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 5.50 for a
book they sell at $49.99 - Bloomberg puts the pound at 2.021 USD at the
moment, so 4.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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Delphi 3? Turnpike 6.05 < http://www.yqcomputer.com/ >/> TP/BP/Delphi/&c., FAQqy topics & links; < http://www.yqcomputer.com/ ;m> clpdmFAQ;
 
 
 

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

Post by Randy Web » Sat, 01 Sep 2007 10:20:35

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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://www.yqcomputer.com/
Javascript Best Practices - http://www.yqcomputer.com/
 
 
 

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

Post by Peter Mich » Sat, 01 Sep 2007 10:50:57


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
 
 
 

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

Post by FAQEdito » Sat, 01 Sep 2007 11:16:59

Peter Michaux said the following on 8/30/2007 9:50 PM:



Unless a consensus from the "regulars" is given, it is going to stay the
way it is because the FAQ is a group FAQ, not a personal FAQ (A concept
that JRS refuses to accept/acknowledge). The way it is now was a
consensus - at the time - and it was the cause of the note about "not
reviewed thoroughly enough" with regards to the Fifth Edition.

Any other thoughts/comments on the Fifth Edition from anybody else so
that it can at least get updated to either reflect no book or the Fifth
Edition?


Yeesh... Corrected locally.

--
Randy
comp.lang.javascript FAQ - http://www.yqcomputer.com/
FAQ Notes: http://www.yqcomputer.com/
ECMAScript Language Specification via FAQ2.6
 
 
 

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

Post by Dr J R Sto » Mon, 03 Sep 2007 00:17:58

In comp.lang.javascript message < XXXX@XXXXX.COM >,
Thu, 30 Aug 2007 21:20:35, Randy Webb < XXXX@XXXXX.COM > posted:

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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL: http://www.yqcomputer.com/ ; - FAQish topics, acronyms, & links.
Plaintext, quoting : see <URL: http://www.yqcomputer.com/ ;
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)
 
 
 

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

Post by Randy Web » Mon, 03 Sep 2007 11:59:14

Dr J R Stockton said the following on 9/1/2007 11:17 AM:

I see that you ignored the question.


I will, after you answer the question.


Of course, nothing I present to you would be considered "honorable" by
you so there isn't much point in me wasting my time trying - yet again -
to explain it to you. I have pointed you to the thread where I explained
that my time was limited for a while and even why it was limited. You
just didn't seem to be able to comprehend what you read.


If the group wants me to give it up, I will. Not a problem with it
either. I have offered to you, several times, a way to "solve the
problem" and to date you have not done anything about it but whine
incessantly about it because you don't want to do anything *but* whine
about it.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://www.yqcomputer.com/
Javascript Best Practices - http://www.yqcomputer.com/