[dojo-contributors] defineClass

Scott J. Miles sjmiles at turboajax.com
Thu Apr 27 02:50:14 EDT 2006

>> using this over more traditional methods
These *are* the traditional methods. I'm simply reducing boilerplate.
Analogy: dojo.extends is a helper method which allows setting up a prototype
with a minimum of pain. Similarly dojo.defineClass is just a helper method
that allows standard JS stuff to be expressed in a compact manner.
The only non-factory concept is the idea of a non-constructor initializer
>> Can you give us a use case or two where there would be a definite
advantage to using this over more traditional methods?

Without define class:
    myClass = function() {
     mySuperclass.call(this); // uh oh, don't forget this or your class is
    dojo.inherits(myClass, mySuperclass);
    dojo.lang.extend(myClass, {
       prop: "value"
With defineClass:
    dojo.defineClass('myClass', mySuperclass, {
     prop: "value";
More compact, easier to understand, no worries about forgetting to call
superclass constructor. 
Additionally, in the constructor you can have a problem because you might be
instantiating an object or you might be creating a prototype for inheriting
from myClass. Separating an instance initializer from the constructor solves
this problem night transparently.
Scott J. Miles
TurboAjax Group


From: dojo-contributors-bounces at dojotoolkit.org
[mailto:dojo-contributors-bounces at dojotoolkit.org] On Behalf Of Tom Trenka
Sent: Wednesday, April 26, 2006 10:24 PM
To: dojo dev.
Subject: Re: [dojo-contributors] defineClass

I don't really mean to continually butt heads here, but I'm still failing to
see the point/need for this?  It seems like it's an alternate way of
creating objects in Javascript, except instead of doing it the way the
language was designed there's basically a number of additional properties
that are added for the sole purpose of being able to get at the immediate

I don't have a problem with having a function that can take an
initialization function and use that in conjunction with a constructor; that
seems like a pretty good mechanism for deserialization from custom
serialization formats in the very least.  But this looks an awful lot to me
like it's trying to force JS to do something that's more common in other

Can you give us a use case or two where there would be a definite advantage
to using this over more traditional methods?


On 4/25/06, Scott J. Miles <HYPERLINK
"mailto:sjmiles at dojotoolkit.org"sjmiles at dojotoolkit.org> wrote: 

We discussed some weeks ago a helper function to define/extend classes. I
propose a function to do that, interested parties can review it here:



P.S. It's really quite simple. I only made a separate page because I figured
Outlook would mangle the formatting, I couldn't get the Wiki to cooperate,
and I wanted to be as clear as possible. 

No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.1.385 / Virus Database: 268.4.6/324 - Release Date: 4/25/2006
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.dojotoolkit.org/pipermail/dojo-contributors/attachments/20060426/7e6b6217/attachment.htm 

More information about the dojo-contributors mailing list