Comments (10)
_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.
from javascript.
@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).
self
is ok because it has a context, just likethis
in JavaScript._this
is ok because you can see it referencesthis
but it has a different context inside the current function.t
is failing at every level. πβΊοΈ
from javascript.
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.
from javascript.
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
?
from javascript.
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.
from javascript.
@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 ... π
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';
}
from javascript.
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 ...
from javascript.
No worries :) Your arguments are good. It seems like a very good explanation. I give up :)
from javascript.
I've overlooked window.self
! @ssorallen thank you for clarifying this!
from javascript.
π @ssorallen for dropping the knowledge bomb about window.self
. I had no idea that existed.
from javascript.
Related Issues (20)
- Support Biome extends HOT 3
- [spam] HOT 1
- [spam]
- [spam] HOT 1
- Multiline comments HOT 3
- [spam]
- [spam]
- TypeError: Failed to load plugin 'jsx-a11y' declared HOT 4
- [spam]
- [spam] HOT 1
- Regarding Updation HOT 4
- [spam] HOT 1
- [spam]
- Link 404 not found HOT 2
- Can we add on quiz sections on this repo too HOT 11
- [spam]
- eslint v9 support
- [spam]
- import/no-extraneous-dependencies shouldn't be a code style rule HOT 25
- Parsing error: Unexpected character '#' HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from javascript.