Mr Speaker

_this = this

var _this = this. It always does. I write it oh-so-often to keep my JavaScript scope hanging around - and the idiom no longer makes me cringe. It's like an old friend. The only problem with my old friend is that Textmate doesn't have the same feelings towards him as I do. He certainly won't elevate him to the status of "keyword" - so he remains white, uncolour-coded, invisible. So in the same vein as my Syntactic artificial sweetener semi-colon reduction strategy, I've decided to fix it. Here's how you make _this equal this.

First, open up Textmate and go to Bundle Editor then Edit Languages:

Next find JavaScript and find the key/value pair for variable.language.js. Here you'll see that super and this are lumped together. All we need to do is add our friend (and an additional pipe symbol) to the list:

That's all that's required to make _this a first-class, syntax-highlighted, citizen. Now your callbacks are nice and legible.

And there we go! You can also add in self if you're not a _this fan.

I am.

And in case you weren't convinced by my compelling arguments - Dave Sims leaves you with this piece of food-for-thought from The Deer Hunter...


  1. Have you considered using some precompiler thingy to skip writing “var _this = this”? I know JSShaper should be able to do this.

    I know it adds some extra cruft to dev work. In case you use a build system already this shouldn’t really matter that much, though.

    Wednesday, May 25, 2011 at 4:34 pm | Permalink
  2. My build system is Textmate->Browser, but I’m looking to cut out the “Textmate” part.

    I love the idea of precompiler bits ‘n’ pieces, but I also love that I can dig up old javascript experiments I wrote in 2002 and they STILL work! Extra steps make it more likely that I’ll never go back to old things: for example, when all the good bits of coffeescript get merged into Harmony, then even my ol’ coffeescript games will be stuck at version 0.1

    Though, not that I ever get beyond v0.1 anyway…

    Thursday, May 26, 2011 at 6:24 pm | Permalink
  3. But JSShaper is interesting… your operator overloading idea is very cool!

    Thursday, May 26, 2011 at 6:55 pm | Permalink
  4. Woo nice idea!

    BTW, I prefer using “self” intead of “_this” , like in python :) but everyone got his conventions ;)

    Monday, June 6, 2011 at 10:02 pm | Permalink
  5. This is this. It isn’t something else.

    Thursday, July 21, 2011 at 7:35 am | Permalink
Captcha! Please type 'radical' here: *
How did you find this thingo? *