question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support use of `this` keyword in static ES6 methods

See original GitHub issue

It’s very common (and safe) to use the this keyword in a static member definition:

class Foo {
  static get baz() { return 'baz'; }

  static bar() {
    console.log(this.baz);
  }
}

We should replace the this keyword with a reference to the class name.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:20
  • Comments:8 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
samouricommented, Aug 4, 2020

Chiming in here to point out that using this within static es6 methods doesn’t throw an error at compile time, which is what I’d prefer for an unsupported feature (vs. breaking at runtime).

0reactions
princedcommented, Jul 12, 2019

It turns out that https://github.com/google/closure-compiler/commit/5b1e5316e5228d6d9f205e28440d67d9ec007f4b only fixes issues like https://github.com/google/closure-compiler/issues/2880 (unknown type of this), but the main problem (collapsing static methods) is still there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

static - JavaScript - MDN Web Docs - Mozilla
The static keyword defines a static method or property for a class, or a static initialization block (see the link for more information ......
Read more >
Static Methods in ES6 - Medium
Note that for static methods, the this keyword references the class. You can call a static method from another static method within the...
Read more >
JavaScript Static Methods
In ES6, you define static methods using the static keyword. The following example defines a static method called createAnonymous() for the Person class:....
Read more >
Static properties and methods - The Modern JavaScript Tutorial
Usually, static methods are used to implement functions that belong to the class as a whole, but not to any particular object of...
Read more >
javascript - ES6: this within static method - Stack Overflow
Under typical circumstances, the this in any call to something.method() will refer to something as long as the function is not an arrow ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found