Digital Web Magazine

The web professional's online magazine of choice.

Scope in JavaScript : Comments

By Mike West

September 11, 2006


Dustin Diaz

September 16, 2006 11:19 PM

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;

Mike West

September 17, 2006 4:15 AM

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.

Sorry, comments are closed.

Media Temple

via Ad Packs