[Javascript] Question about best method of creating inheritance in javascript

Amanda Birmingham lists at imladris.com
Tue Apr 23 11:08:45 CDT 2002


Hello, List,
I'm new here, so please forgive any faux pas.  I've been gearing up to 
write a very large OO javascript program, and have therefore been 
investigating the different merits of possible ways of subclassing in 
Javascript (yeah, I know "class" is a bit of a misnomer, but you know what 
I mean :)

So far, I've seen these three methods:
* make superclass constructor a method of subclass and call it inside the 
subclass constructor (without the "new").  This takes the subclass object 
and extends its "this" with the superclass info.

* inside the subclass constructor, create a new instance of the superclass. 
Then extend it and return that extended superclass as the subclass instead 
of the the "this".

* assign a new superclass object to the prototype property of the subclass 
constructor function.

Question 1:  when you create an instance of a subclass that inherits some 
stuff, do all of these methods result in you *also* creating objects of all 
the classes it inherits from?

Question 2: If *so*, what is the advantage of the prototype method (which, 
as I understand it, is the real, *approved* way of doing inheritance in 
javascript), since it seems that using prototype has problems of blocking 
access to methods of the superclass that have the same name as objects of 
the subclass  (unless you use something like Kevin Lindsey's workaround) ?

Answers, advice, and/or descriptions of other methods I've overlooked would 
be gratefully welcomed.  Thanks,

Amanda Birmingham
Web Application Developer




More information about the Javascript mailing list