Both you and the blog entry are confused here. That's not an optimization, that's how scope works in JavaScript. All implementations MUST do that. The quote he referenced is completely irrelevant to the behavior that his code has.
Closures don't snapshot the state of the program.. they just reference the environment. If you change the value of a variable in that environment, it's going to show up as changed in the closure. In some languages, like Erlang, it's impossible for that variable to change because it's a single assignment language.. but this behavior happens in JavaScript, Python, Ruby, etc. -bob On 10/17/06, troels knak-nielsen <[EMAIL PROTECTED]> wrote: > > Apart from the obvious, that Bob had event.stop(); in his example > (Which you could add if you need it, but most likely you don't), there > is one peculiar detail. > Some javascript interpreters optimize closures like this, so they can > be reused if called multiple times. This would happen if you assign > the closure in a loop, and might lead to oddities. If you use > mochikit's Iter functions, this won't be a problem, and frankly it's > fairly exotic any way. > Here's a blog post explaining the problem : > http://joust.kano.net/weblog/archive/2005/08/08/a-huge-gotcha-with-javascript-closures/ > > But in general, "my" solution will work, and since it's shorter and > easier to read, I think it's preferable. > > On 10/17/06, Chris W. Parker <[EMAIL PROTECTED]> wrote: > > At Saturday, October 14, 2006 10:06 AM troels knak-nielsen <> said: > > > > > Or simply: > > > > > > connect('target1', 'onclick', function() { > > > change_html('target1', 'new html'); > > > }); > > > > Sorry I'm replying to this just now. I don't get a lot of time to play > > with this everyday. > > > > Is there any functionality that I may be missing out on if I do it this > > way instead of returning the function the way Bob suggested? > > > > > > > > Thanks, > > Chris. > > > > > -- > troels > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "MochiKit" 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/mochikit -~----------~----~----~----~------~----~------~--~---
