|
Depends on what you extend, and what you are
creating.
Extending nothing (implicit inheritance from
Object), no need to have default properties?
Then nothing.
Extending nothing, but need to have customizable
properties?
Pass them into the constructor. Some would
argue you should utilize an init instead since it's easy to re-initialize a
class instance with new data vs. recreating the whole thing.
Example:
a = new Cow();
a.init(name, dateOfBirth);
a.init(anotherName, anotherDate);
vs bad way:
a = new Cow(name, dateOfBirth);
a = new Cow(anotherName, anotherDate):
Most Model classes have public properties so you
don't even need a constructor or init function since you can just set the
properties. Others have public methods accomplishing the same
thing.
The third facet is if you are extening something
that automatically calls your constructor or some other sub
function.
For example, extending mx.core.UIObject,
UIComponent, or View have a bunch of built-in functions that run
automatically. You don't need to even define a constructor, although it's
nice as a best practice. For example, if you extend UIObject, you're init
function is called for you automatically, and at that point, if you passed in an
initialization object, those properties are already set.
So bottom line, I rarely use constructors.
Most of the teaching programming books I've seen show those as examples, but I
don't ever use them in the real-world, and if I do, it's only for intrinsic
model classes (Array, Date, etc.). None of my View classes ever take
parameters because currently, Flash Player requires factory methods to create
things vs. a = new MovieClip().
Hope that helps.
----- Original Message -----
From: Rick Schmitty
Sent: Monday, September 26, 2005 1:34 PM
Subject: [flexcoders] When to use constructor vs init? class foo { function foo(){ // do stuff here like add event listeners? } function init(){ super.init() // do stuff here like add event listeners? } } Also should super.init() be first or last inside of init()? I've seen examples using either, and sometimes both Thanks! -- Flexcoders Mailing List FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com YAHOO! GROUPS LINKS
|
- Re: [flexcoders] When to use constructor vs init? JesterXL
- Re: [flexcoders] When to use constructor vs init? Aldo Bucchi
- Re: [flexcoders] When to use constructor vs init? Rick Schmitty

