问题
I have a question in regards to an aspect of event-driven programming. I am not sure whether the code for event handlers and event listeners should be completely independent from each other.
From my understanding, event listeners are attached to some type of application (let's say a GUI), and their only responsibility is to capture user input.
A message dispatcher then captures that event, and sends it to the appropriate event handler.
So, it seems to me that dependencies should not exist between the event listener code and the event handler code. Am I thinking of this correctly? I haven't really found a good event-driven book, and most of the articles I read about event-driven programming sound a little biased or naive.
The best document I could find is a bit old.
回答1:
i think the listener is just some code to gather data and send data to logic code when the event triggers.
the logic code just care about the input data and do task, it is event independent
the code is like this:
function sayHello(name) { // this code is independent
console.log("hello" + name);
}
element.addEventListener("click", function () {
var name = "gather data dependent on the event and context"; // code here is event depentdent
sayHello(name);
}, false);
so: logic is independent. how to get data is dependent
来源:https://stackoverflow.com/questions/20307377/event-handlers-event-listeners-independent-of-one-another