Inside your handlers
object, this
refers to handlers
, not the class instance.
A reduced version:
class MyClass {
handlers = {
name: 'handlers',
foo() {
console.log(this.name);
}
}
name = 'MyClass';
}
var x = new MyClass();
x.handlers['foo'](); // Prints 'handlers', not 'MyClass';
My recommendation would be to pass the class instance to the handler functions as an additional argument, or even better to just refactor this pattern entirely. As written it doesn't really make sense -- you're creating a new handlers
object for every instance of the class, even though they belong on the prototype (because they don't hold instance data).