arrow function 을 이용한 bind 이슈 해결
상황
export default class Field {
constructor(carrotCount, bugCount) {
...
this.field.addEventListener('click', this.onClick);
}
onClick(event) {
const target = event.target;
if (target.matches('.carrot')) {
target.remove();
sound.playCarrot();
this.onItemClick && this.onItemClick('carrot');
} else if (target.matches('.bug')) {
this.onItemClick && this.onItemClick('bug');
}
}
...
}해결
1. 기존에 사용하던 방법
2. 새로 알게된 방법
arrow function 을 사용한 더 우아한 방법
Last updated