Javascript - 将参数传递给非匿名函数

Vla*_*žić 2 javascript

我有这个功能:

 $("#btn").click(function(e,someOtherArguments)
   {  //some code
    e.stopPropagation();});
Run Code Online (Sandbox Code Playgroud)

它工作,但如果我已命名函数我不能使用,e因为它是未定义的.

var namedFunction= function(e,someOtherArguments) 
{
 //some code
  e.stopPropagation();
 }
$("#btn").click(namedFunction(e,someOtherArguments));
Run Code Online (Sandbox Code Playgroud)

我想使用它,namedFunction因为有几个按钮使用它.

moo*_*e99 5

或者:

$("#btn").click(namedFunction);
Run Code Online (Sandbox Code Playgroud)

要么:

$("#btn").click(function(e,someOtherArguments){ 

  namedFunction(e, someOtherArguments);

});
Run Code Online (Sandbox Code Playgroud)

  • +1.除此之外,@ impeRAtoR,如果你说`$("#btn").click(anyFunction)`jQuery将调用`anyFunction()`,其中_wants_传递的参数数量,而不是你可能的参数数量或者可能没有声明`anyFunction()`接受.(实际上在JS中,函数根本不需要显式声明命名参数,因为它可以通过`arguments`对象访问它们.) (3认同)