I am trying to write an ES6 class for an interactive calendar on my current project.
The Class looks similar to the below:
class Calendar {
constructor (s) {
this.eventButtons = s.eventButtons;
this.eventButtons.forEach(button => button.addEventListener('click', this.method1);
this.eventBoxes = s.eventBoxes;
method1 (e) {
e.preventDefault();
this.method2(e.target.href);
}
method2 (url) {
console.log(url);
}
}
export default Calendar;
I know that the context of the 'this' keyword has changed from the constructor to the button which has been clicked within the method1 function. However I don't know how to keep the context of the button and the constructor within the same function. I tried changing the button event listener code to the following:
this.eventButtons.forEach(button => button.addEventListener('click', this.method1).bind(this);
But this just switches the context of the 'this' keyword to the constructor rather than the button. I need to use both in my function.
Any ideas? I'm hoping this is quite a common issue?
You could create a closure that would send the event and the button. The closure would keep the this context and send the button as well
button => button.addEventListener('click', event => this.method1(event, button))
Since you are using ES6, have you tried using an arrow function?
An arrow function expression has a shorter syntax than a function expression and does not bind its own this, arguments, super, or new.target. These function expressions are best suited for non-method functions, and they cannot be used as constructors.
method1 = (e) => {
e.preventDefault();
this.method2(e.target.href);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With