简体   繁体   English

与“标准”功能和事件处理程序使用相同的功能

[英]Use same function as “standard” one and as an event handler

I have a function 我有一个功能

function foo(event, var1) {
    // do something...
}

I am using this function in some event bubbling, but I also need to use as a normal function that's called by me from my code. 我在某些事件冒泡中使用此函数,但我还需要将其用作我从代码中调用的普通函数。 Event parameter is used only with event bubbling. Event参数仅用于事件冒泡。 What to pass to event from my own code to get this to work? 要从我自己的代码传递给event什么才能使其正常工作?

Ps I have a feeling this is not good design, but that's the way things turned out. 附言:我觉得这不是一个好的设计,但是事实就是这样。 We'll change it when time comes, but for now, it's gotta stay like this. 时间到了,我们将对其进行更改,但就目前而言,必须保持这种状态。

Thank you. 谢谢。

If var1 is always passed to the function then you should switch the order of params. 如果var1始终传递给函数,则应切换参数的顺序。

If your function goes foo(var1, event) then it doesn't matter if event is empty, the function will still work. 如果您的函数进入foo(var1, event)那么event是否为空都无关紧要,该函数仍然可以正常工作。 This has to do with the concept of optional params . 这与可选参数的概念有关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM