Great article! I'm looking forward to the next post. Manu.
On Fri, Feb 29, 2008 at 2:28 PM, T.J. Crowder <[EMAIL PROTECTED]> wrote: > > @Rob: Thanks for the links. > > @All: I promised to write back when I finally finished that post; > here it is for what it's worth: > http://niftysnippets.blogspot.com/2008/02/closures-are-not-complicated.html > > -- T.J. :-) > > On Feb 27, 11:24 pm, RobG <[EMAIL PROTECTED]> wrote: > > On Feb 28, 2:44 am, "Miller, Raul D" <[EMAIL PROTECTED]> wrote: > > > > > T.J. Crowder wrote: > > > > That's easy to do. :-) He's using a closure to construct and return > > > > another closure; it's the inner one that gets assigned to the > > > > onSuccess handler and retained, the outer one is run immediately and > > > > discarded. If you look carefully at the code defining the onSuccess > > > > handler, you'll see that the entire outer closure is wrapped in > parens > > > > -- the opening paren is right after "onSuccess:", and then right at > > > > the end of the closure you see the closing paren followed by > "(item)" > > > > -- that "(item)" tells you he's *executing* the outer closure right > > > > then, not just creating it. When it runs, the outer closure creates > > > > the inner closure and returns it. *That's* what gets assigned to > > > > onSuccess, the reference to the inner closure. The outer closure > has > > > > only the param it needs (item) and no local variables, so the inner > > > > closure only preserves the information it really needs, not all of > the > > > > local variables where all of this is running. > > > > A function declaration or statement within another function > > declaration or statement forms a closure with the outer function's > > execution object when the code is executed. You don't have to > > actually reference any of the outer function's local variables to > > create the closure, it's added to the inner function's scope chain > > always. > > > > function foo() { > > var x = function(){}; > > > > } > > > > The function assigned to x has a closure to the execution object > > created when foo() is called. There are no references to foo's local > > variables so there is no way to access them, but the closure is still > > created. This technique is used to create private variables in > > javascript. > > > > <URL:http://javascript.crockford.com/private.html> > > > > [...] > > > > > Have I overlooked an important issue? > > > > Perhaps. > > > > -- > > Rob > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
