Hacker News new | ask | show | jobs
by triceratops 977 days ago
This is how everyone wrote classes in JS[1] before the class keyword was added. Most people didn't want to futz with constructor functions and prototype chains. Eventually a class keyword was added to the language.

"Having to write:

this.progressBar.addEventListener(this.handler.bind(this)); is much worse compared to:

progressBar.addEventListener(handler);"

If handler is declared as:

class Foo {

  handler = () => {
    // handle
  }
}

Then there's no need to bind it to this when using it later.[2]

You can just write

this.progressBar.addEventListener(this.handler);

Additionally if handler doesn't reference any other class properties and isn't referenced outside the class, it can also be a function in the module.

class Foo {

  constructor() {
    this.progressBar.addEventListener(handler)
  }
}

function handler () {

  // handle
}

1. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

2. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...