I'm probably developing too late, I should have thought of the missing
binding. But thanks a lot for the nice explanations, I was missing the
theoretical part here (I'm just a lazy reader).

Cheers,
Matthieu

On Jul 18, 1:08 am, "Mark Holton" <[EMAIL PROTECTED]> wrote:
> Matt ...in addition to utilizing Prototype's bind in the manner Chris
> described ... you might also want to reference/read up on
> 'closures<http://www.jibbering.com/faq/faq_notes/closures.html>'
> in 
> JavaScript<http://www.amazon.com/JavaScript-Definitive-Guide-David-Flanagan/dp/0...>,
> as JS closures are related to Prototype's bind.
>
> cheers-
>
> On 7/18/07, Christophe Porteneuve <[EMAIL PROTECTED]> wrote:
>
>
>
> > Matt a écrit :
> > > I'm having some difficulty with the periodical executer, I use it
> > > within one of my objects and when I use 'this' it doesn't actually
> > > reference the object. Here is the example:
>
> > Binding 101, Matt.  Change your initialize line:
>
> > > new PeriodicalExecuter(this.pollRadio, 2);
>
> > To this:
>
> > > new PeriodicalExecuter(this.pollRadio.bind(this), 2);
>
> > BTW, you may probably want to store that PE ref somewhere in case you
> > need to stop the PE from running at some point.
>
> > > However I have no idea with the reference to 'this' isn't the right
> > > one and there's probably a better way. Any idea? If there's no better
>
> > Look athttp://prototypejs.org/api/function/bindor grab some good JS
> > book (or mine, for that matter) and read on "this", execution contexts,
> > binding, etc.
>
> > Essentially, the trick is: whenever you pass a method reference around,
> > it loses its "this", okay?  The only way for a method to be properly
> > bound to an instance is to call it directly on the instance (e.g.
> > "this.method(a, b)" or "someObj.method(a, b)".  Anything else will lose
> > the binding.
>
> > Prototype provides a bind method for functions that spews out an
> > anonymous function (not associated to your original object in itself, so
> > you can safely pass it around), and this new function essentially makes
> > sure to call your original method on its proper instance.
>
> > --
> > Christophe Porteneuve a.k.a. TDD
> > "[They] did not know it was impossible, so they did it." --Mark Twain
> > Email: [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 rubyonrails-spinoffs@googlegroups.com
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