6

I'm not a frontend dev, but I recall that a few years ago, the this keyword was commonplace in frontend codebases.

In recent years, I haven't seen this get used anymore. In the last few frontend codebases I've worked on, I haven't seen it get invoked at all. Did it get obsoleted by recent developments in JavaScript, or is this just an idiosyncrasy of the developers I've been working with?

2 Answers 2

3

With the introduction of anonymous functions, binding this is generally not needed. In addition using frontend frameworks such as react or vue or angular or .... doesn't leave a room for needing to explicitly bind this; it is taken care of implicitly inside the framework implementation so you can write declarative code.

But it is not obsolete for many reasons:

In cases you need to use normal functions instead of anonymous functions (for hosting for ex) you may need to bind this.

Svelte for ex, requires a strong understanding of this, because you can use it in lifecycle methods and to reference custom components.

Finally the most common place where you would use this is in classes and utility JavaScript files.

3
  • While your reasons for it are completely correct, this sounds very much like it is obsolete in that language/framework improvements mean it is no longer needed. Apr 17, 2022 at 9:55
  • I don't think it sounded like it is obselete because i mentioned one case where it is used. In utility classes and js files for ex. Also svelte which is a frontend framework still require a strong understanding of this because it is a minimalistic framework that doesnt have many abstractions
    – EEAH
    Apr 17, 2022 at 10:12
  • "Mostly obsolete" then :) - with any language feature, you can always find an edge case where it's needed but if it's not needed for the majority of use cases then I'd be happy calling it obsolete. Apr 17, 2022 at 10:22
-3

As a C# developer I noticed there are two camps.

  1. It is not required, not using it is shorter so we do not use it because we like to be concise.
  2. It explicitly signals you are addressing a class member, not a local variable or argument or constant, hence it adds value to the code and prevents mistakes so we apply it consistently.

I am in the second camp, when it is up to me I consistently apply it. But I must say I do not miss it that much anymore now that the IDE tells me with colors that I am looking at a class member. I would say this is the main reason some people are no longer that anal about it.

So ultimately it is a matter of personal preference. I would have applauded it if it would have been mandatory, if the compiler/interpreter would just not accept references to class members without this. It now takes the local if it is there and falls back to a class member if there is no local, which is not a good thing for a programming language in my book.

1
  • 13
    this in JavaScript is quite different from this in C#. It is usually not optional, and I've never seen anyone use it unless necessary. Contrary to the "verbose clarity" in C#, it is generally a source of confusion even for intermediate-level JS programmers. Apr 17, 2022 at 15:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.