Izzy, the fact that element is defined in the scope of the element means
it's already in the closure:
var foo
var bar = function(){
//foo is maintained in the scope of this function whether or not you refer
to it.
};
On Mon, May 2, 2011 at 2:43 PM, Izzy <[email protected]> wrote:
> Don't want to bring this too back on topic, but please, please, don't
> do this:
>
> > var MyClass new Class({
> > initialize: function(element){
> > element.addEvent('click', function(){
> > this.setStyle('color', 'red'); //here "this" points to the
> element,
> > and can catch you of guard
> > element.setStyle('color', 'red'); //much better; you don't need to
> > use "this" here because you already have a pointer to the element
> > });
> > }
>
> Referring to the element from the closure scope instead of the event
> handler scope immediately results in terrible practice and can result
> in a memory leak.
> Luckily at least MT 1.2.x provides the .destroy() function to help
> clean up messes like this, but I'm not so sure about 1.1.1.
>
> Having the event handler be a member of the class and using a proper
> binding technique like Class.Binds is much better.
>