Bas van Gaalen a écrit :
> For instance, I was expecthing that the following would be possible
> and that the 'this' in the initialize-function would refer to
> MyObject. I guess I misunderstood the note in the blog as this does
> not seem to be the case..
> 
> var MyObject = {
>   initialize: function() {
>     alert(this);
>   }
> }
> 
> document.observe('dom:loaded', MyObject.initialize);

No, you misunderstood.  Scope correction means in an unbound event 
handler, "this" will refer to the element you're calling observe *on*. 
In the code above, you're calling observe on *document*, so "this" will 
refer to document.

When you need to have methods as event handlers you need to specifically 
bind; there's actually no technical way around it at all: JS does not 
equip us with surefire means of detecting that the function we're 
dealing with is a method (which would let us get rid of 
bindAsEventListener, for instance), and even if it did, you wouldn't 
necessarily want to bind to the owner object.

-- 
Christophe Porteneuve aka TDD
[EMAIL PROTECTED]

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Spinoffs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to