我很难删除事件侦听器类型的currying函数。
// I register first, at some time I want to remove using freezeHighlight but it doesn't working (without currying function it's working like a charm)
const privateMethods = {
highlighted (index) {
return function (event) {
event.target.setAttribute('filter', 'url(#glow)')
// need param index later
}
}
}
register (node, index) {
node.addEventListener('mouseover', privateMethods.highlighted(index))
}
freezeHighlight (node) {
node.removeEventListener('mouseover', privateMethods.highlighted)
}
是否可以删除事件侦听器类型的currying函数,或者我应该采取解决方法?
答案 0 :(得分:1)
您需要记住您创建的处理程序,以便以后将其删除。
const handlers = {};
const privateMethods = {
highlighted (index) {
// return the saved handler if we've been called before
// or create a new handler, save it, and return it.
return handlers[index] || (handlers[index] = function (event) {
event.target.setAttribute('filter', 'url(#glow)')
// need param index later
});
}
}
register (node, index) {
// add the handler
node.addEventListener('mouseover', privateMethods.highlighted(index))
}
freezeHighlight (node, index) {
// will remove the handler
node.removeEventListener('mouseover', privateMethods.highlighted(index))
}
答案 1 :(得分:0)
Columns=
您认为var listener
register (node, index) {
listener = privateMethods.highlighted(index)
node.addEventListener('mouseover', )
}
freezeHighlight (node) {
node.removeEventListener('mouseover', listener)
}
是侦听器。不,这不对。 privateMethods.highlighted
的返回值是侦听器。