事件处理程序和事件侦听器彼此独立吗? [英] Event Handlers & Event Listeners Independent of one Another?

查看:87
本文介绍了事件处理程序和事件侦听器彼此独立吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于事件驱动编程的一个方面,我有一个问题.我不确定事件处理程序和事件侦听器的代码是否应该彼此完全独立.

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.

根据我的理解,事件侦听器连接到某种类型的应用程序(例如GUI),它们的唯一职责是捕获用户输入.

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.

推荐答案

我认为侦听器只是一些代码,用于在事件触发时收集数据并将数据发送到逻辑代码.

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

代码如下:

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

这篇关于事件处理程序和事件侦听器彼此独立吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆