Javascript: Why to use "_this" as a reference to "this"?

Created on 23 Aug 2013  路  10Comments  路  Source: airbnb/javascript

I personally use t to store my reference to this.

I understand that self resembles PHP or PERL, but it is not the same thing. It's bad.
that is even worse...

...but why _this? It looks similar to "private" variables. It's at least confusing...

I'm puzzled in here :) Plese explain.

Most helpful comment

_this was originally chosen because Airbnb used a mix of CoffeeScript and JavaScript, and the CoffeeScript fat arrow, =>, created a reference like var _this = this in the compiled JS. Rather than choose another convention, the JS guide adopted the CS convention.

History aside, window.self exists in all browsers, and so it is available by just referencing self in JavaScript. For that reason alone, I would stay away from using self.

@serbanghita makes good arguments for _this. Choose something and be consistent, but pick something more descriptive than t! Future developers who look at your code will thank you.

All 10 comments

You need to explain why self is bad. t is short and confusing, if you have a long code and review it after one month, I would probably get puzzled by t because it's not self explanatory.

I have already explained it. self is not the same thing as it is in PERL or PHP. Yet ,for example, I see my friends who come from these languages to use it, they say it's easier for them to get a firm grasp over the language, but they forget about programmers with different background. I have used couple of times by myself...

About t - I believe it to be less intrusive than the others.

BTW ;) @serbanghita you haven't answered my question ;) Why we should use _this?

@op1ekun - I didn't understand the part with "Perl and PHP". I don't know Perl, but in PHP the only system variables of importance that contains self are $_SERVER['PHP_SELF'] which refers to the current url path, and self:: used in a context of a class (with static methods).

  1. self is ok because it _has a context_, just like this in JavaScript.
  2. _this is ok because you can see it references this but it has _a different context_ inside the current function.
  3. t is failing at every level. :-1: :relaxed:

Yeah I know about PHP (this was my first serious ;P language), but thank you for your brief explanation.
That somewhat proves my point.
Some people use self because it looks like something they use in a different language, but they are not the same thing in different languages.
That's why I decided for javascript ONLY solution (I haven't seen t anywhere else). So it doesn't matter from which background you coming from you WILL NOT GET CONFUSED.

BTW I'm not sure if you are aware, but in here https://github.com/airbnb/javascript#naming-conventions people have already decided that self is bad ;) but there is no explanation why.

@op1ekun i think you will get confused in the future using t, because it's short. You're breaking the rule: "Avoid single letter names.". I mean if it suits you, it's okay.

I did these kind of mistakes in the past and because sometimes us programmers have short bursts of _holy inspiration_ and write great scripts without any documentation, and after a period, coming back on the code for a simple code refactoring you tend to go like: "what and who the f**k is t'? Now imagine that you're working with a team ... and they have to review the code ... :disappointed:

I think self was avoided because it might sound like a static variable or it's harder to distinguish in the code.

Take a look at the following example, if you replace _this with self you might get confused when reading the setAttribute line.

IMHO: _this points to this, and the _ signifies that it's something temporary and should be kept private.
Note: I used w and h variables like that on purpose, imagine having a var t somewhere in the code ... it gets confusing.

var Loader = function(w, h, alt){
   this.w = w;
   this.h = h;
   this.alt = alt;
}

Loader.loadImage = function(){

   var img = new Image();

   var _this = this; // or use 'self'
   img.onload = function(){
      // Set attributes from the constructor.
      this.setAttribute('alt', _this.alt);
   }
   img.src = '/img/myImage.png';

}

Sorry I forgot to mention that self is used by jQuery. It's a matter of choice, but t is still bad in every way ...

No worries :) Your arguments are good. It seems like a very good explanation. I give up :)

_this was originally chosen because Airbnb used a mix of CoffeeScript and JavaScript, and the CoffeeScript fat arrow, =>, created a reference like var _this = this in the compiled JS. Rather than choose another convention, the JS guide adopted the CS convention.

History aside, window.self exists in all browsers, and so it is available by just referencing self in JavaScript. For that reason alone, I would stay away from using self.

@serbanghita makes good arguments for _this. Choose something and be consistent, but pick something more descriptive than t! Future developers who look at your code will thank you.

I've overlooked window.self! @ssorallen thank you for clarifying this!

:+1: @ssorallen for dropping the knowledge bomb about window.self. I had no idea that existed.

Was this page helpful?
0 / 5 - 0 ratings