Hi,

> So this solves that problem-- but global variables are "evil"..
> BUT..  this is in the context of a closure, so I wonder if it's less
> evil?

The global variable is not affected by the fact it's being created by
code in a closure, you're still (implicitly) creating a `photos`
property on the `window` object, which is going to get you into
trouble some day. :-)

Although you could solve the global problem easily enough (just
declare a `photos` var within your outer closure, just above your
Class.create call -- then that will be the one at the top of the scope
chain and you won't create an implicit global, just a class-wide
global), this approach of using a variable that basically means "the
current instance" has a couple of issues other than the global
variable problem. For instance, what if you have Photo A and Photo B,
and A calls B? They're sharing a global "current instance" variable,
and so when A calls B, B will overwrite it. So then A has to remember
to overwrite it again when B's call completes (which would require
`this` to be set correctly, which defeats the purpose). So I don't
think that pattern is workable beyond fairly small-scale scenarios.

In my work (which involves quite extensive classes), I find it easier
to just manage `this`. That does mean using Function#bind periodically
(but Function#bind is fairly efficient), and it means I have to get
downright procedural at times when calling private functions -- either
I pass in `this`, or I call the function via Function#call (which is a
lot like doing something procedural). But it soon becomes quite
natural and doesn't set me up too badly to get myself in trouble. The
biggest error I tend to make is forgetting to use #call when I should,
but that error typically shows up right away and is easily fixed (and
I make that mistake a *lot* less now thanks to naming conventions and
practice).

FWIW,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com


On Apr 9, 8:46 am, patrick <patrick99...@gmail.com> wrote:
> Hi everyone,
>
> My question is--  quite often I do something like this:
>
> var Photos = (function() {
>     var Photos = Class.create({
>         initialize: function() {
>                 var photos = this;
> //............etc
>     }
>
> return Photos;
>
> })();
>
> and then when I have class methods, I have redefine a local variable
> to reference the class, such as:
>
>        foo: function() {
>           var photos = this;
>
> }
>
> I prefer to set a variable to 'this', because the context of this ends
> up getting very confusing at times, and requires lots of .bind,
> etc..... So anyway, doing the whole 'var photos = this' in every
> method ends up being rather redundant and so I have found myself
> doing:
>
> var Photos = (function() {
>     var Photos = Class.create({
>         initialize: function() {
>                 photos = this;
>          // etc
>         },
>
>         foo: function() {
>           photos.do_something;
>         }
>     });
>
>    return Photos;
>
> })();
>
> So this solves that problem-- but global variables are "evil"..
> BUT..  this is in the context of a closure, so I wonder if it's less
> evil?  Or if there is a better way to do what I am doing?  I am not
> sure how you guys do this sort of thing...  So, I'd love to hear your
> advice.
>
> Thanks.
> -patrick

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptacul...@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.

Reply via email to