News for hackers. Stuff that matters.
Re:Finally someone makes sense of JavaScript's this keyword by statictype on Saturday April 23, 2011 @09:39AM (#2476793)
I used to always run into trouble when trying to use 'this' inside a closure defined within an object's method.

Now, I just put

    var that = this;
at the top of very method and use 'that' everywhere in the function.
Re:Finally someone makes sense of JavaScript's this keyword by CoffeeDregs on Saturday April 23, 2011 @12:47PM (#2477106)
FWIW: that's exactly what ExtJS does:

    initComponent: function() {
        var me = this;
        me.callParent();
        ...
Thanks for explaining the rationale; I'd never paid enough attention to understand why they were doing that.
Re:Finally someone makes sense of JavaScript's this keyword by swax on Saturday April 23, 2011 @01:02PM (#2477127)
same here, I use 'this' rarely. always start new function classes with var self = this; and never use 'this' again.
Re:Finally someone makes sense of JavaScript's this keyword by jrockway on Saturday April 23, 2011 @11:45PM (#2478221)
Aha! I also do "var self = this;" but feel really bad about it, like I'm missing something obvious about the language. I guess I'm not.
Re:Finally someone makes sense of JavaScript's this keyword by msy on Sunday April 24, 2011 @05:00AM (#2478542)
You're not alone, every time I do this I feel like I'm going wrong somewhere. I know it's avoidable via binding/currying/proxying but often those solutions end up looking uglier and are harder to follow.
Re:Finally someone makes sense of JavaScript's this keyword by mikepurvis on Saturday April 23, 2011 @10:58AM (#2476908)
I do the same thing with var me.

Another way is jQuery.proxy(), though that works best for calling a method with the correct context.

Re:Finally someone makes sense of JavaScript's this keyword by masklinn on Saturday April 23, 2011 @11:24AM (#2476964)
> Another way is jQuery.proxy()

Or the numerous ``bind`` functions and methods (which do the same thing).

In fact, ES5's Function prototype even grew a ``bind`` method[0], which is used to partially apply a function (both to bind it to a ``this`` and to bind it to an arbitrary number of arguments).

Some libraries (e.g. underscore) also have a shortcut to bind all methods of an object (or only a subset of those)[1], handy when using methods as event or deferred callbacks

[0] https://developer.mozilla.org/en/JavaScript/Reference/Global...

[1] http://documentcloud.github.com/underscore/#bindAll

Re:Finally someone makes sense of JavaScript's this keyword by jt2190 on Saturday April 23, 2011 @04:53PM (#2477576)
I just use 'this' as much as possible. It helps me keep track of which object/scope 'this' is referencing. Creating a second reference to the object/scope referenced by 'this' doesn't really change anything.

Something I also find helpful is reminding myself that I'm not just writing a function, but a method for the current object (referenced by 'this').

Re:Finally someone makes sense of JavaScript's this keyword by SimHacker on Sunday April 24, 2011 @01:57PM (#2479271)
Yes it does change things. You're not understanding that "this" is a keyword, not a variable reference, and as such, it is not treated the same in closures. The "this" KEYWORD is dynamically bound at runtime, not lexically scoped like every other variable. It is impossible to close over "this", yet it's what you usually want to do. That is why people use ugly work-arounds like "var me = this;". So "this" is a pitfall and language design flaw that traps many JavaScript programmers (even experienced ones) into thinking they're writing correct code that looks like it does one thing, which actually does a totally (and sometimes subtly) different thing.
Re:Finally someone makes sense of JavaScript's this keyword by jt2190 on Monday April 25, 2011 @12:09PM (#2481723)
The article is about 'this', not about creating closures.

I agree that in the case where you're creating a closure, you need to create a reference to the scope referenced by 'this' in order to 'enclose' it, however, that doesn't make it a best practice, all the time, as many responses are suggesting.

Re:Finally someone makes sense of JavaScript's this keyword by DjDarkman on Saturday April 23, 2011 @03:14PM (#2477332)
I do the same it's really expressive IMHO, it's strange at start but when you understand it it's awesome.
Re:Finally someone makes sense of JavaScript's this keyword by davej on Saturday April 23, 2011 @12:26PM (#2477069)
Spot on, I do the same (but I use $this as the variable name).
Re:Finally someone makes sense of JavaScript's this keyword by techiferous on Saturday April 23, 2011 @03:48PM (#2477398)
I use the $ sigil to differentiate between variables that are DOM elements and variables that are jQuery-wrapped DOM elements.
Re:Finally someone makes sense of JavaScript's this keyword by SpiralLab on Saturday April 23, 2011 @05:17PM (#2477613)
Same here.

     var _this = this;    //vanilla
     var $this = $(this); //jQuery
Though, I have encountered people who are really against the $var for stylistic reasons - so ymmv.