re: Is `this` in Javascript bad? VIEW POST

VIEW PARENT COMMENT VIEW FULL DISCUSSION
 

const self = this is not solving the problem that this could be bound ambiguously. It is simply solving the problem where each function would have their own this. E.g.

function a () {
  const self = this;
  return function() {
    return self.x;
  }
}

a.call({ x: 3 })(); // 3

I like using bind to solve this problem tho:

function a () {
  return (function() {
    return this.x;
  }).bind(this);
}

a.call({ x: 3 })(); // 3

But of course the nicest way would be using arrow functions:

function a () {
  return () => this.x;
}

a.call({ x: 3 })(); // 3
code of conduct - report abuse