最佳答案
I'd like to dispatch an event that will pass some data to any event listeners that listen on that event.
Considering a function that fires an event:
function click() {
const x = 'foo'
document.dispatchEvent(new CustomEvent('clicked'))
}
click()
How can I pass custom data to the event listener?
document.addEventListener('clicked', function(e) {
console.log(x) // logs "foo"
})