将函数名称传递给另一个函数 [英] passing a function name to another funtion

查看:76
本文介绍了将函数名称传递给另一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我想尝试的系统,这需要我从aspx

页面调用通用函数来执行一项任务,然后我想要

用该任务的结果调用另一个函数。


这样的东西:

我的aspx页面中的


< input type = button onClick =" doFirstFunction(''secondFunctionName'');">


然后在我的.js文件中:


var functionToCall ='''';


函数doFirstFunction(functionName){


functionToCall = functionName


//做最初的常见任务

sResult = xmlHttp.responseText


//现在调用第二个功能,将sResult传递给它


}


现在我正在使用''switch''并手动添加可能的值

''functionName''然后手动指导,但我想制作

" doFirstFunct离子"自我维护,即无需更新,如果

我稍后会添加更多第二功能。


我以为我可以使用


eval(''functionToCall(''+ sResult +'')'');


但到目前为止这还不行。


任何想法?

I have a system that I want to try, which requires me, from an aspx
page, to call a generic function to work perform one task, then i want
to call another function with the results of that task.

something like this:

in my aspx page:
<input type=button onClick="doFirstFunction(''secondFunctionName'');">

then in my .js file:

var functionToCall = '''';

function doFirstFunction(functionName) {

functionToCall = functionName

// do the initial, common task
sResult = xmlHttp.responseText

//now call the second function, passing sResult to it

}

Right now I''m using ''switch'' and manually adding possible values for
''functionName'' then directing manually, but I''d like to make
"doFirstFunction" self-maintaining, i.e. without having to update it if
I add more ''second'' functions later.

I thought I could use

eval(''functionToCall(''+sResult+'')'');

but this doesn''t work so far.

any ideas?

推荐答案



Kevin Blount写道:

Kevin Blount wrote:
我有一个我想尝试的系统,这需要我从aspx
页面调用泛型函数来执行一项任务,然后我想要
用这个任务的结果调用另一个函数。

在我的aspx页面中:

< input type = button onClick =" doFirstFunction (''secondFunctionName'');">
I have a system that I want to try, which requires me, from an aspx
page, to call a generic function to work perform one task, then i want
to call another function with the results of that task.

something like this:

in my aspx page:
<input type=button onClick="doFirstFunction(''secondFunctionName'');">




删除'''它将准备好使用功能参考

(假设在点击时存在secondFunctionName:

& lt; input type =" button" onclick =" firstFunction(secondFunction);">

....

function firstFunction(ptr){

//做东西

ptr();

}


另外(如果你更喜欢句柄字符串) )所有现有的功能都是通过窗口对象进行的,所以:


< input type =" button" onclick =" firstFunction(''secondFunction'');">

....

函数firstFunction(str){

//做东西

窗口[str]();

}



Remove apos'' and it will become ready to use function reference
(presuming that the secondFunctionName exists at the moment of click:
<input type="button" onclick="firstFunction(secondFunction);">
....
function firstFunction(ptr) {
// do stuff
ptr();
}

Alternatively (if you prefer handle strings) all existing functions are
tunelled through the window object, so:

<input type="button" onclick="firstFunction(''secondFunction'');">
....
function firstFunction(str) {
// do stuff
window[str]();
}


VK说以下是2006年4月21日下午4:22:
VK said the following on 4/21/2006 4:22 PM:
Kevin Blount写道:
Kevin Blount wrote:
我有一个系统我想尝试,这需要我,来自aspx
页面,调用一个通用函数来执行一个任务,然后我想用另一个函数调用该任务的结果。

这样的事情:

在我的aspx页面中:
< input type = button onClick =" doFirstFunction(''secondFunctionName'');">
删除'''它将准备就绪使用函数引用
(假设在点击时存在secondFunctionName:
< input type =" button" onclick =" firstFunction(secondFunction);">
....
功能firstFunction(ptr){
//做东西
ptr();
}
I have a system that I want to try, which requires me, from an aspx
page, to call a generic function to work perform one task, then i want
to call another function with the results of that task.

something like this:

in my aspx page:
<input type=button onClick="doFirstFunction(''secondFunctionName'');">
Remove apos'' and it will become ready to use function reference
(presuming that the secondFunctionName exists at the moment of click:
<input type="button" onclick="firstFunction(secondFunction);">
....
function firstFunction(ptr) {
// do stuff
ptr();
}




ptr未定义,除非这是名字函数。

或者(如果你更喜欢句柄字符串)所有现有的函数都是通过window对象进行的,所以:

< input type ="按钮" onclick =" firstFunction(''secondFunction'');">
....
函数firstFunction(str){
//做东西
window [str ]();
}



ptr is undefined unless that is the name of the function.
Alternatively (if you prefer handle strings) all existing functions are
tunelled through the window object, so:

<input type="button" onclick="firstFunction(''secondFunction'');">
....
function firstFunction(str) {
// do stuff
window[str]();
}




这是你提到的唯一方法。并且''

必须在初始函数调用上。

-

Randy

comp .lang.javascript常见问题解答 - http://jibbering.com/faq &新闻组每周

Javascript最佳实践 - http://www.JavascriptToolbox .com / bestpractices /



That is the only way to do it that you have mentioned. And the apos''
have to be on the initial function call.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/




Randy Webb写道:

Randy Webb wrote:
function firstFunction(ptr){
//做东西
ptr();
}
function firstFunction(ptr) {
// do stuff
ptr();
}



ptr是未定义的,除非这是名字功能。



ptr is undefined unless that is the name of the function.




你对此持肯定态度吗? ;-)


< html>

< head>

< title> F2F< / title>

< meta http-equiv =" Content-Type"

content =" text / html; charset = iso-8859-1">

< script type =" text / javascript">

function f1(ptr){

alert(''f1'');

ptr();

}

function f2(){

alert(''f2'');

}

< / script>

< / head>


< body>

< p style =" cursor:pointer;颜色:#0000FF; text-decoration:下划线

onClick =" f1(f2);">点击我< / p>

< / body>

< / html>



Are you positive about it? ;-)

<html>
<head>
<title>F2F</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function f1(ptr) {
alert(''f1'');
ptr();
}
function f2() {
alert(''f2'');
}
</script>
</head>

<body>
<p style="cursor:pointer; color:#0000FF; text-decoration:underline"
onClick="f1(f2);">Click me</p>
</body>
</html>


这篇关于将函数名称传递给另一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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