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

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

问题描述

我最近一直在研究活动听众,我想我终于知道了。基本上,它们是对另一个对象的方法调用的函数。我的问题是,为什么在调用函数时创建一个事件监听器可以正常工作?



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



如果您有另一个适合更多使用的示例,请让我知道。也许特定的语言不会从中受益匪浅? (Javascript,PHP,ASP?)

解决方案

您可能并不总是控制正在进行调用的代码。或者即使是这样,您也不想在该代码中引入依赖关系。在这种情况下,代码更好地触发事件,并允许您控制的代码或应该具有依赖关系的代码来监听事件并相应地执行。



例如,也许您正在创建一个将被其他人使用的库。他们没有源代码,或者在某种程度上不能/不应该修改它(或不应该)。您的文件说明在具体情况下提出具体事件。然后,他们可以反过来回应这些事件。



或者您的企业中有一些域名库。你可以控制它们,并且可以对它们进行修改,但在架构上它们通常被认为是正在工作的,因为它们当前被编码,不应该被改变。 (不想发生一轮质量检查来重新验证更新的代码,代码属于另一个部门,他们不希望您更改它等)并且您处于您想要的位置代码能够在不同的环境/环境中做不同的事情。如果该代码引发和相关的事件,您可以将代码挂钩(和/或相应地进行交换),而不必混淆该代码。



只是一对快速的例子,我相信别人有更多的。


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?

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.

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.

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天全站免登陆