Either works, but I find the latter to be cleaner. Also, this is not valid
syntax:
this.items[] = item;
You'd have to do
this.items.push(item);
For what it's worth, I try to limit the amount of functionality in my
initialize methods (and, indeed, all my methods). I like my methods to very,
very simple things. initialize is, for me, about storing the initial state.
Anything that ads or alters anything I put in a method.
So, let's say I had a slideshow, I would do this:
var SlideShow = new Class({
Implements: Options,
options: {
slides: []
},
initialize: function(options){
this.setOptions(options);
this.addSlides(this.options.slides);
},
slides: [],
addSlides: function(slides){
$$(slides).each(this.addSlide.bind(this));
},
addSlide: function(slide){
this.slides.include($(slide)); //don't allow duplicates
}
});
The interesting things:
1) my initialize method allows for the possibility that the class will be
initialized without any slides. this means that the slides passed into the
class are options (if my class *had* to have slides on initialize, I'd make
slides an argument).
2) my initialize method sets options and then calls other methods to do
things with those options (and also, if there were any, other arguments).
3) I have methods to process these values, which allows me to process them
later if I need to, or to process more.
4) Whenever I have a method that does something like "processThingies" I'll
usually also write a "processThingy" method, too. You could really do it
either way. You cold have all your logic in either method and just pass
arguments to whichever one contains your logic (i.e. processThingy can just
push a single item wrapped in an array to processThingies, or the other way
around). This is really just to make the interface nicer.
5) I like to extend my classes, so whenever possible I like granular methods
so I can later do something like this:
var ImgSlideShow = new Class({
Extends: SlideShow,
options: {
imgUrls: []
},
initialize: function(options){
this.parent(options);
this.options.imgUrls.each(this.addImg.bind(this));
},
addImg: function(url){
this.addSlide(new Element('img', {src: url}));
}
});
By having all my logic in granular methods I can insert new logic pretty
much anywhere.
You might find this useful:
http://www.mootorial.com/wiki/mootorial/09-howtowriteamootoolsclass
On Tue, Oct 7, 2008 at 3:13 PM, Michael Duff <
[EMAIL PROTECTED]<[EMAIL PROTECTED]>
> wrote:
>
> so nutron,
>
> is it better to do
>
> $$('.someclass').each(function(item, index) {
> this.items[] = item;
> //other stuff with item....
> });
>
> OR
>
> this.items = $$('.someclass');
>
> this.items.each(function(item,index) {
> //some stuff here
> });
>
>
> *this only applies to the first time I will be accessing these items,
> any other time I would access this.items*
>
> Mike
>
>
> On Oct 7, 5:17 pm, nutron <[EMAIL
> PROTECTED]<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=1304959&i=0>>
> wrote:
>
> > I disagree. Calling $$ twice on the same selector is not very efficient.
> > Whenever possible, only call $$ once. $ is much less forgiving on speed.
> > Calling either $$ or $ on a collection of elements or a single element
> > (respectively) has no cost. So:
> > var el = $(id);
> > $(el).whatever() //no expense
> > $(id); //cheaper than the first time and but slightly more costly
> >
> > var els = $$(selector);
> > $$(els).whatever() //no expense
> > $$(selector); //nearly as expensive as the first time
> >
> > On Tue, Oct 7, 2008 at 2:13 PM, Nathan White <
> > [EMAIL
> > PROTECTED]<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=1304959&i=1>
> <[EMAIL
> PROTECTED]<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=1304959&i=2>>
>
> >
> >
> >
> > > wrote:
> > > Your code snippet is the best approach.
> >
> > > Mootools is fast at retrieving elements that have already been
> 'touched'.
> > > The biggest culprit in performance is the DOM traversal.
> >
> > > On Tue, Oct 7, 2008 at 5:06 PM, Michael Duff <[EMAIL PROTECTED]<
> http://n2.nabble.com/user/SendEmail.jtp?type=node&node=1304791&i=0>
> > > > wrote:
> >
> > >> Hey Everyone,
> >
> > >> I have a question about coding style, I will be using the same "items"
>
> > >> over and over throughout a class I am writing, so would it be better
> > >> to place these items in an array for later use?
> >
> > >> $$('.someclass').each(function(item, index) {
> > >> this.items[] = item;
> > >> });
> >
> > >> then anytime I need to loop through them I can just use this.items
> > >> instead of $$, does anyone know if its more expensive to store
> > >> references to those items or for mootools to go find them each time?
> >
> > >> Mike
> >
> > > ------------------------------
> > > View message @
> > >http://n2.nabble.com/cheaper-to-use-array-or-%24%24%28%29-tp1304776p1...
>
> > > To start a new topic under MooTools Users, email
> > > [EMAIL
> > > PROTECTED]<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=1304959&i=3>
> <[EMAIL
> PROTECTED]<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=1304959&i=4>>
>
> > > To unsubscribe from MooTools Users, click here< (link removed) >.
> >
> > -----
> > The MooTools Tutorial: http://www.mootorial.comwww.mootorial.com
> > CNET Clientside: http://clientside.cnet.comclientside.cnet.com
> > --
> > View this message in context:
> http://n2.nabble.com/cheaper-to-use-array-or-%24%24%28%29-tp1304776p1...
> > Sent from the MooTools Users mailing list archive at Nabble.com.
>
>
> ------------------------------
> View message @
> http://n2.nabble.com/cheaper-to-use-array-or-%24%24%28%29-tp1304776p1304959.html
> To start a new topic under MooTools Users, email
> [EMAIL PROTECTED]<[EMAIL PROTECTED]>
> To unsubscribe from MooTools Users, click here< (link removed) >.
>
>
>
-----
The MooTools Tutorial: http://www.mootorial.com www.mootorial.com
CNET Clientside: http://clientside.cnet.com clientside.cnet.com
--
View this message in context:
http://n2.nabble.com/cheaper-to-use-array-or-%24%24%28%29-tp1304776p1305006.html
Sent from the MooTools Users mailing list archive at Nabble.com.