为什么在函数调用上使用事件监听器? [英] Why use event listeners over function calls?

查看:18
本文介绍了为什么在函数调用上使用事件监听器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在研究事件监听器,我想我终于搞定了.基本上,它们是在另一个对象的方法上调用的函数.我的问题是,为什么在调用函数时创建一个事件监听器就可以了?

I've been studying event listeners lately and I think I've finally gotten them down. Basically, they are functions that are called on another object's method. My question is, why create an event listener when calling the function will work just fine?

例如,我想调用 player.display_health(),当它被触发时,方法 player.get_health() 应该被触发并存储,以便 display_health() 可以访问它.为什么我应该使用事件侦听器而不是简单地调用函数?即使 display_health() 在另一个对象中,这对我来说仍然不是问题.

Example, I want to call player.display_health(), and when this is fired, the method player.get_health() should be fired and stored so that display_health() has access to it. Why should I use an event listener over simply calling the function? Even if display_health() were in another object, this still doesn't appear to be a problem to me.

如果您有另一个更适合用法的示例,请告诉我.也许特定语言并没有从中受益那么多?(Javascript、PHP、ASP?)

If you have another example that fits the usage better, please let me know. Perhaps particular languages don't benefit from it as much? (Javascript, PHP, ASP?)

推荐答案

您可能并不总是能够控制执行调用的代码.或者即使您是,您也不想在该代码中引入依赖关系.在这种情况下,最好让代码触发事件并允许您控制的代码或应该具有依赖关系的代码侦听事件并采取相应措施.

You might not always be in control of the code that's doing the calling. Or even if you are, you don't want to introduce dependencies into that code. In cases like that, it's better for the code to fire an event and allow the code you do control, or the code that should have the dependency, to listen for the event and act accordingly.

例如,也许您正在创建一个可供其他人使用的库.他们没有源代码,或者以某种方式不能/不应该能够修改它(或者不应该).您的文档指出在特定情况下会引发特定事件.然后,他们可以反过来响应这些事件.

For example, perhaps you're creating a library that will be used by other people. They don't have the source code or in some way can't/shouldn't be able to modify it (or shouldn't have to). Your documentation states that specific events are raised under specific circumstances. They can then, in turn, respond to those events.

或者您的企业中可能有一些域库.您确实可以控制它们并可以修改它们,但从架构上讲,它们通常被认为是按照当前编码的方式工作的,不应更改.(不想招致一轮 QA 来重新验证更新的代码,该代码属于另一个部门,他们不希望您更改它,等等)而且您处于您想要的位置代码能够在不同的情况/环境下做不同的事情.如果该代码引发了相关的事件,您可以将您的代码挂接到其中(和/或相应地换出),而不必弄乱该代码.

Or perhaps you have some domain libraries in your enterprise. You do control them and can modify them, but architecturally they're generally considered to be working as they currently are coded and shouldn't be changed. (Don't want to incur a round of QA to re-validate the updated code, the code belongs to another department and they don't want you to change it, etc.) And you're in the position where you want that code to be able to do different things in different circumstances/environments. If that code raises and event where relevant, you can hook your code into it (and/or swap out accordingly) without having to mess with that code.

只是几个简单的例子,我相信其他人还有更多.

Just a couple quick examples, I'm sure others have more.

这篇关于为什么在函数调用上使用事件监听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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