问题描述
伙计,
我敢肯定这可以合法完成,而不是通过交易的技巧 - 我
希望有人可以提供帮助。
我正在写一个''工具''(一个函数),可以在任何一个中使用
我的项目当它完成时,它可以调用成功,或者失败
函数。这些成功的名称或失败函数会有所不同,
我想知道如何将函数名称传递给我的工具,
和我的工具如何使用该名称调用该函数...
粗略地说我希望有类似的东西
函数my_engine(success_function_name ,failure_function_name)
{
//这里处理代码咀嚼数据
//然后......
if(myResult == true)
{success_function_name(); }
else
{failure_function_name(); }
}
有人有任何意见/建议吗?我确定有一个js方法
我可以使用但我在O''Reilly JavaScript
口袋参考中找不到它的引用。 ..
所有帮助,通过新闻组(所以都可以学习)将不胜感激,
谢谢,
Randell D.
function callFunction(name)
{
var f = new Function(name +"()");
f();
}
但也许只是传递函数本身更方便
作为参数...
罗伯特。
-
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.
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.
--
Stephane Moriaux et son [moins] vieux Mac
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>
这篇关于如何让一个函数动态调用另一个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!