Giter Club home page Giter Club logo

Comments (10)

ssorallen avatar ssorallen commented on May 7, 2024 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.

serbanghita avatar serbanghita commented on May 7, 2024 6

@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. πŸ‘Ž ☺️

from javascript.

serbanghita avatar serbanghita commented on May 7, 2024

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.

op1ekun avatar op1ekun commented on May 7, 2024

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.

op1ekun avatar op1ekun commented on May 7, 2024

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.

serbanghita avatar serbanghita commented on May 7, 2024

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

serbanghita avatar serbanghita commented on May 7, 2024

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.

op1ekun avatar op1ekun commented on May 7, 2024

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

from javascript.

serbanghita avatar serbanghita commented on May 7, 2024

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

from javascript.

spikebrehm avatar spikebrehm commented on May 7, 2024

πŸ‘ @ssorallen for dropping the knowledge bomb about window.self. I had no idea that existed.

from javascript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.