Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
How to have extensible event handler methods in a JavaScript class?

I have a JavaScript class that runs code when a click occurs. To make that code extensible, I've put it in a separate handleClick() method.

However, that's not working ideally because I can't access properties of the class instance via this. In the snippet below, this.bar is undefined when handleClick() is being called.

class Foo {
  constructor() {
    this.bar = 42;
    window.addEventListener("click", this.handleClick);
  }

  handleClick() {
    console.log("doing something", this.bar);
  }
}

new Foo();

How to fix this?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

To avoid the undefined issue, create a new property that holds a bound version of the event handler function using Function.prototype.bind() and use that as the event listener. In the snippet below, that's the handleClickFn property:

class Foo {
  constructor() {
    this.bar = 42;
    window.addEventListener("click", this.handleClickFn);
  }

  handleClickFn = this.handleClick.bind(this);
  handleClick() {
    console.log("doing something", this.bar);
  }
}

new Foo();

Now the class can be extended and everything works as expected:

class Foo {
  constructor() {
    this.bar = 42;
    window.addEventListener("click", this.handleClickFn);
  }

  handleClickFn = this.handleClick.bind(this);
  handleClick() {
    console.log("doing something", this.bar);
  }
}

class FooChild extends Foo {
  handleClick() {
    console.log("doing something else", this.bar);
  }
}

new FooChild();

Another approach is to bind the listener to the class instance itself (this) and implement the special handleEvent() method that the browser calls automatically, as described in the MDN docs for addEventListener():

class Foo {
  constructor() {
    this.bar = 42;
    window.addEventListener("click", this);
  }

  handleEvent(e) {
    if (e.type === "click") {
      this.handleClick();
    }
  }

  handleClick() {
    console.log("doing something", this.bar);
  }
}

class FooChild extends Foo {
  handleClick() {
    console.log("doing something else", this.bar);
  }
}

new FooChild();

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!