Giter Site home page Giter Site logo

Why "_this" and not "that" about javascript HOT 3 CLOSED

airbnb avatar airbnb commented on May 5, 2024
Why "_this" and not "that"

from javascript.

Comments (3)

spikebrehm avatar spikebrehm commented on May 5, 2024

Haha, good question. This is a side effect of the conversion of the Rendr source code from CoffeeScript to JavaScript. There is still cleanup that can happen to make it read more like human-written JS. I'm fine with that, and also it may be possible to avoid the alias altogether for certain calls, by either passing in this as the context as the last argument, or by using Function.prototype.bind:

someArray.map(function(element) {
  ...
}, this);
this.app.on('someEvent', function() {

}, this);
doSomething(function() }
  this.someMethod();
  ...
}.bind(this));

from javascript.

hshoff avatar hshoff commented on May 5, 2024

There are 2 reasons behind using _this over that.

The big one for us is we've used CoffeeScript for some larger projects and CoffeeScript compiles this aliases to _this. We wanted to protect ourselves in the event we decide to ditch the CoffeeScript files and move forward with the compiled JS source. This helps with consistency with the rest of the JS in our codebase.

The other reason is readability. Our team agreed on _this being the easier reference to understand when you jump into a larger JS project opposed to trying to track down the that assignment.

The important part for any large JS codebase with lots of folks working on the project is consistency. So If your team decides "that" is easier to read and everyone already uses it. That's fine. Just go with what's best for everyone.

This is another example of why I named our style guide "A Mostly Reasonable Approach to JavaScript"

from javascript.

spikebrehm avatar spikebrehm commented on May 5, 2024

Oops @amiorin, I had just assumed that this issue was opened on the airbnb/rendr project. Excuse the nonsensical answer.

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.