传递一个函数作为参数,然后在jquery函数中执行它 [英] pass a function as a parameter and then execute it in a jquery function

查看:1272
本文介绍了传递一个函数作为参数,然后在jquery函数中执行它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个像这样的函数:

  function setSomething(){
make some stuff;
}

,然后执行另一个函数:

  generalFunction(par1,par2,par3){
do other stuff;
在par3中调用的执行函数;
}

好吧,如果我写这样的东西不起作用: p>

  c = setSomething(); 
generalFunction(a,b,c);

那么,调用一个函数作为另一个函数的参数然后在里面执行的方法是什么? / p>

我希望我很清楚。



任何帮助都会感激。



提前感谢您的关注。

解决方案

省略括号,然后可以调用参数作为您的generalFunction函数内的函数。

  setSomething(){
//做其他东西
}

generalFunction(par1,par2,par3){
// do stuff ...

//可以调用参数一个函数(因为它是!)
par3();
}

generalFunction(a,b,setSomething);


I was wondering which is the way to make this simple (and maybe stupid) thing with jQuery.

I have a function like this:

function setSomething() { 
    make some stuff; 
}

and then another function like this:

generalFunction(par1, par2, par3) { 
    do other stuff; 
    execute function called in par3;    
}

Well, if I write something like this it doesn't work:

c=setSomething(); 
generalFunction(a, b, c);

So what's the way to call a function as a parameter of another function and then execute it inside?

I hope I was clear enough.

Any help will be appreciated.

Thank you in advance for your attention.

解决方案

leave out the parentheses , you can then call the parameter as a function inside your "generalFunction" function.

setSomething(){
   // do other stuff  
}

generalFunction(par1, par2, par3) { 
    // do stuff...

    // you can call the argument as if it where a function ( because it is !)
    par3();
}

generalFunction(a, b, setSomething);

这篇关于传递一个函数作为参数,然后在jquery函数中执行它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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