Event Handlers & Event Listeners Independent of one Another?

我们两清 提交于 2019-12-13 04:22:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!