> 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.

Correct, using an inner function is usually okay, except when that
function is going to be attached to an element that would be inside
its closure scope.

I know Mootools helps you out by abstracting away some of the danger
as long as you use Element.destroy to clean up your messes, but not
using inner functions if possible is a good practice (at least imo).

Which is why my suggestion was to use a member function/method as the
event handler, ala:

MyClass = new Class({
  initialize: function(element) {
    $(element).addEvent('click', this.clickHandler.bind(this));
  },
  clickHandler: function(event) {
    $(event.target).set('text', 'you clicked me');
  }
};

I personally prefer this method over using Class.Binds (http://
mootools.net/docs/more/Class/Class.Binds) since it shows exactly what
is happening, rather than making it easy for someone to not understand
that methods need to be bound correctly in order to have *this* stay
correct.

Reply via email to