Scope in JavaScript : Comments
September 11, 2006
Comments
Oh thank goodness someone finally wrote this. I was afraid one day that I’d have to write something similar, but then only do a half-ass job at it. This will be an excellent reference post where I can point people to when trying to explain scope.
I enjoyed your subtitles “Where am I? And who are you?... this? What’s that?” – You have no idea how many times that pops into my head. The end result (because of a closure) always end up the same:
var that = this;
The point Matthias makes is an excellent one. If you’re detaching event handlers, you’ll have to have some sort of cache that holds the function references you’ve used, since `bind` does indeed give you a new function each and every time. That’s what the YUI library does, I believe.
And speaking of Yahoo, I’m glad I saved you some time, Dustin. I’m downloading your new screencast right now, actually, so it’s nice to know the appreciation isn’t completely one-way. ;) `var that = this;` is a good rule of thumb for JavaScript objects in general; closures just make more sense when the variable names mean something.

You can use this
feed to keep up with the comments made on this article.