Subject=Re: Re: Better way to maintain this reference on event listener functions

# Mark Kennedy (9 years ago)

[delete please] mistake

# Andrea Giammarchi (9 years ago)

Raising a concern about addEventListener and its context probably made me think it was rather a WHATWG concern.

Anyway, I don't know why you had to point out the class keyword ... I mean ....

class SomeClass {
  constructor(someNode) {
    someNode.addEventListener('click', this);
  },
  onclick(e) {
    alert(this.constructor.name); // SomeClass
  },
  handleEvent(e) {
    this['on' + e.type](e);
  }
}

There you go

Best

# Domenic Denicola (9 years ago)

To give some clarity to "whose concern is it", the idea of it being an ES concern is that this is a concern for all APIs that accept a callback. addEventListener is a simple example.

I think ES has already given an answer to this question: Function.prototype.bind. I'm not sure what the OP was hoping to accomplish that could not already be accomplished with bind. Any possible mechanism (e.g. syntactic sugar for a (thisArg, function) pair) would essentially duplicate what bind does under the hood.