this有没有办法在不丢失事件处理程序提供的绑定的情况下将附加参数部分应用于函数。bind需要一个值,因此将 null 作为第一个参数会丢失对否则会存在this的 DOM 元素的引用。在不丢失对元素的引用的情况下<button>Click</button>提供诸如allow之类的附加参数会很好bind
const button = document.querySelector('button')
function once(...args) {
console.log(...args, this) // {prop: 'val'}, event, `Window`
button.removeEventListener('click', bound)
}
const extraArgs = {
prop: 'val'
}
const bound = once.bind(null, extraArgs)
button.addEventListener('click', bound)
<button>Click</button>
在这个例子中,可以通过button作为第一个参数传入 bind 来实现效果,但我感兴趣的是不要“丢失”绑定this,而不是用对元素的引用替换它。