Suppose we want to put an event handler, and remove it right there:
let button = document.getElementById('button'); function func() { console.log('Π‘ΡΠ°Π±ΠΎΡΠ°Π»ΠΎ'); } button.addEventListener('click', func); button.removeEventListener('click', func); <button id="button">ΠΠ»ΠΈΠΊΠ½ΠΈ</button> Great, func didn't work.
But what happens if the handler has functions with this?
let button = document.getElementById('button'); let objWithFunc = { func() { console.log('Π‘ΡΠ°Π±ΠΎΡΠ°Π»ΠΎ'); }, start() { function auxiliaryFunc() { this.func(); } // ΠΠΎΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΠΌΡΡ .bind(this) ΠΈΠ½Π°ΡΠ΅ Π±ΡΠ΄Π΅Ρ ΠΎΡΠΈΠ±ΠΊΠ° button.addEventListener('click', auxiliaryFunc.bind(this)); // ΠΠΈ ΠΎΠ΄ΠΈΠ½ ΠΈΠ· ΡΠ»Π΅Π΄ΡΡΡΠΈΡ
ΠΌΠ΅ΡΠΎΠ΄ΠΎΠ² Π½Π΅ ΡΠ½ΠΈΠΌΠ΅Ρ ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ button.removeEventListener('click', auxiliaryFunc); button.removeEventListener('click', auxiliaryFunc.bind(this)); } } objWithFunc.start(); <button id="button">ΠΠ»ΠΈΠΊΠ½ΠΈ</button> How to remove the handler in this case?