如何通过 addEventListener 传递语句?
我一直在尝试通过动作脚本中的 addEventListener 事件传递参数,例如...
target.addEventListener("pComp", rakeSoil(target));
但我收到错误。
我尝试用谷歌搜索,但没有成功:/
如果你这样做的话,谢谢你的回复:)
Iv been trying to pass arguments through an addEventListener event in actionscript such as...
target.addEventListener("pComp", rakeSoil(target));
but i get errors.
Iv tried to google but no luck :/
Thanks for replying if you do :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
目标已作为事件的一部分传递,
event.currentTarget
或event.target
将是您想要的。如果您想要传递其他内容,请创建自定义事件。将属性添加到自定义事件。
The target is already passed as part of the event, either
event.currentTarget
orevent.target
will be what you want.If you want something else passed, create a custom event. Add the property to the custom event.
尝试添加一个附加方法作为事件侦听器:
Try adding an additional method as your event listener:
这就是你想要的:
rakeSoil 是一个一流的函数(或闭包),当事件被调度时,它将被调用,并且你可以访问其中的“目标”。
编辑:
看看 Closure(计算机科学)
How this is what you want:
rakeSoil is a first class function(or closure), when event is dispatched, it will be invoked, and you can access 'target' in it.
EDIT:
Have a look at Closure (computer science)
我一直发现匿名函数带来的麻烦比其价值更大。我将简单地遵循标准事件处理程序代码布局。它更正式,需要更多的努力,但是没有歧义,并且当您一年后返回它时,它的可读性要高得多(减少了令人头疼的持续时间):
虽然,现在我更仔细地观察它,为什么让这个对象做一些 Target 应该能够在内部自行处理的事情?
I have always found anonymous functions to be more trouble than they are worth. I would simply follow the standard event handler code layout. It's more formal and takes a little more effort up front, but there is no ambiguity and it is far more readable when you return to it a year from now (reduces head-scratching duration):
Although, now that I look at it more closely, why are you having this object do something that Target should be capable of handling internally on its own?