Why to use "_this" as a reference to "this"?
See original GitHub issueI 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:
- Created 10 years ago
- Comments:10 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

_thiswas originally chosen because Airbnb used a mix of CoffeeScript and JavaScript, and the CoffeeScript fat arrow,=>, created a reference likevar _this = thisin the compiled JS. Rather than choose another convention, the JS guide adopted the CS convention.History aside,
window.selfexists in all browsers, and so it is available by just referencingselfin JavaScript. For that reason alone, I would stay away from usingself.@serbanghita makes good arguments for
_this. Choose something and be consistent, but pick something more descriptive thant! Future developers who look at your code will thank you.@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
selfare$_SERVER['PHP_SELF']which refers to the current url path, andself::used in a context of a class (with static methods).selfis ok because it has a context, just likethisin JavaScript._thisis ok because you can see it referencesthisbut it has a different context inside the current function.tis failing at every level. 👎 ☺️