如何让一个函数动态调用另一个函数? [英] How can I have one function call another function dynamically?

查看:105
本文介绍了如何让一个函数动态调用另一个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



伙计,


我敢肯定这可以合法完成,而不是通过交易的技巧 - 我

希望有人可以提供帮助。


我正在写一个''工具''(一个函数),可以在任何一个中使用

我的项目当它完成时,它可以调用成功,或者失败

函数。这些成功的名称或失败函数会有所不同,

我想知道如何将函数名称传递给我的工具,

和我的工具如何使用该名称调用该函数...


粗略地说我希望有类似的东西

函数my_engine(success_function_name ,failure_function_name)

{

//这里处理代码咀嚼数据


//然后......

if(myResult == true)

{success_function_name(); }

else

{failure_function_name(); }

}


有人有任何意见/建议吗?我确定有一个js方法

我可以使用但我在O''Reilly JavaScript

口袋参考中找不到它的引用。 ..


所有帮助,通过新闻组(所以都可以学习)将不胜感激,


谢谢,

Randell D.

解决方案

Randell D.写道:


我想要要知道如何将函数名称传递给我的工具,
以及我的工具如何使用该名称来调用函数...




function callFunction(name)

{

var f = new Function(name +"()");

f();

}


但也许只是传递函数本身更方便

作为参数...


罗伯特。


Randell D.写道:


伙计,
<我相信这可以做到合法而不是通过交易的技巧 - 我希望有人可以提供帮助。

我正在写一个工具(一个功能),可以在任何一个地方使用我的项目。完成后,它可以调用成功或失败
功能。这些成功或失败函数的名称会有所不同,
我想知道如何将函数名称传递给我的工具,
以及我的工具如何调用函数,用这个名字......

粗略地说我希望有类似的功能my_engine(success_function_name,failure_function_name)
//
//处理代码在这里咀嚼数据

//然后......
if(myResult == true)
{success_function_name(); }


试试:

函数(success_function_name);

调用

my_engine(''成功'',''失败'');




{success_function_name}

或?

{eval(success_function_name)}

并致电:

my_engine(''success()'',''failure()'');

else
{failure_function_name(); }
}




-

Stephane Moriaux et son [moins] vieux Mac


>我想知道如何将函数名称传递给我的工具,

以及我的工具如何使用该名称来调用函数...




这很简单。只需传递*函数引用*,而不是函数名。


< script type =" text / javascript">

函数f1(fun1,fun2 ){

fun1();

fun2()

}


函数f2() {

alert(''f2'')

}


函数f3(){

alert(''f3'');

}


函数init(){

f1(f2,f3) ;

}


window.onload = init;

< / script>



Folks,

I''m sure this can be done legally, and not thru tricks of the trade - I
hope someone can help.

I''m writing a ''tool'' (a function) which can be used generically in any
of my projects. When it completes, it can call a success, or a failure
function. The names of these success, or failure functions will differ,
and I''d like to know how I can pass the name of a function to my tool,
and how my tool can call the function, using that name...

Roughly speaking I want to have something like

function my_engine(success_function_name, failure_function_name)
{
// Processing code here chews data

// Then...
if(myResult==true)
{ success_function_name(); }
else
{ failure_function_name(); }
}

Anybody got any ideas/suggestions? I''m sure there was a js method that
I could use but I can''t find reference to it in my O''Reilly JavaScript
pocket reference...

All help, via the newsgroup (so all can learn) will be greatly appreciated,

Thanks,
Randell D.

解决方案

Randell D. wrote:


and I''d like to know how I can pass the name of a function to my tool,
and how my tool can call the function, using that name...



function callFunction(name)
{
var f = new Function(name+"()");
f();
}

But maybe it would be more convenient to just pass the function itself
as a parameter...

Robert.


Randell D. wrote:


Folks,

I''m sure this can be done legally, and not thru tricks of the trade - I
hope someone can help.

I''m writing a ''tool'' (a function) which can be used generically in any
of my projects. When it completes, it can call a success, or a failure
function. The names of these success, or failure functions will differ,
and I''d like to know how I can pass the name of a function to my tool,
and how my tool can call the function, using that name...

Roughly speaking I want to have something like

function my_engine(success_function_name, failure_function_name)
{
// Processing code here chews data

// Then...
if(myResult==true)
{ success_function_name(); }
try :
Function(success_function_name);
with calling
my_engine(''success'', ''failure'');

or
{ success_function_name }
or ?
{ eval(success_function_name) }
and calling :
my_engine(''success()'', ''failure()'');
else
{ failure_function_name(); }
}



--
Stephane Moriaux et son [moins] vieux Mac


> I''d like to know how I can pass the name of a function to my tool,

and how my tool can call the function, using that name...



It is very simple. Just pass *function references*, not function names.

<script type="text/javascript">
function f1(fun1,fun2) {
fun1();
fun2()
}

function f2() {
alert(''f2'')
}

function f3() {
alert(''f3'');
}

function init(){
f1(f2,f3);
}

window.onload = init;
</script>


这篇关于如何让一个函数动态调用另一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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