question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
ssorallencommented, Aug 23, 2013

_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.

6reactions
serbanghitacommented, Aug 23, 2013

@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. 👎 ☺️
Read more comments on GitHub >

github_iconTop Results From Across the Web

Using the this Keyword (The Java™ Tutorials > Learning the ...
The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter. For example,...
Read more >
'this' reference in Java - GeeksforGeeks
'this' is a reference variable that refers to the current object. Following are the ways to use 'this' keyword in java :
Read more >
How to use the C# "this" Reference | Pluralsight
The this keyword is used to reference the current instance of a class, or an object itself, if you will. It is also...
Read more >
Why and when to reference | Referencing explained | Library
Referencing is an important part of academic work. It puts your work in context, demonstrates the breadth and depth of your research, and...
Read more >
References, C++ FAQ - Standard C++
How can you reseat a reference to make it refer to a different object? Why does C++ have both pointers and references? When...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found