Thanks for answers an comments.
Naming function is a solution when using closure patterns, for instance.
But not for bounded functions :
function name references the original function, not the bounded instance.
Some example :
function listener(s,ev){
"use strict";
if (ev.target.value == s){
console.log("<bounded> I found " + s+ ", Bye !");
// Doesn't work : listener references original (not bounded) function
ev.target.removeEventListener(ev.type, listener);
// Who am I ?? ;-)
} else {
console.log("<bounded> still waiting for " +s + " " + new Date());
}
}
window.addEventListener('load', function(){
i = document.querySelector('input');
i.addEventListener("change",listener.bind(i,"abc"));
i.addEventListener("change",listener.bind(i,"def"));
});
I wrote this ugly workaround :
function autoRefBind(f, ...args){
var o = {};
o.me = f.bind(o,...args);
return o.me;
}
function listener(s,ev){
"use strict";
if (ev.target.value == s){
console.log("<bounded> I found " + s+ ", Bye !");
ev.target.removeEventListener(ev.type, this.me);
} else {
console.log("<bounded> still waiting for " +s + " " + new Date());
}
}
window.addEventListener('load', function(){
i = document.querySelector('input');
i.addEventListener("change",autoRefBind(listener,"abc"));
i.addEventListener("change",autoRefBind(listener,"def"));
});
But it's very ... ugly : 'this' has to be this "stupid" object {me : }