使用功能 [英] Working with functions
问题描述
我想要一个函数将另一个函数作为参数,并执行
它作为一行代码。我在下面放了一个简化的例子,只是为了说明这个想法。有没有人知道这样做的语法,或者是否
完全可能是更好的方法?
//示例
doSomething(5,10,functionA,functionB);
函数doSomething(x,y,myFunctionA,myFunctionB){
if(x> ; y){
myFunctionA();
} else {
myFunctionB();
}
}
函数functionA(){
//一些代码
}
函数functionB(){
//一些代码
}
I want a function to take another function as a parameter, and execute
it as a line of code. I put a simplified example below, just to
illustrate the idea. Does anyone know the syntax to do this, or is there
perhaps a better approach entirely?
// EXAMPLE
doSomething(5, 10, functionA, functionB);
function doSomething(x, y, myFunctionA, myFunctionB) {
if (x > y) {
myFunctionA();
} else {
myFunctionB();
}
}
function functionA() {
// some code
}
function functionB() {
// some code
}
推荐答案
我怀疑有一个更好的方法,但不知道你的基本意图是什么,这很难说......但你应该看看
eval功能可以满足您的需求。
Jim
I suspect there is a better approach, but without knowing what your
underlying intention is it''s hard to say... but you should look at the
eval function which will do what you want.
Jim
dx27s写道:
dx27s wrote:
我想要一个函数将另一个函数作为参数,然后执行
它作为一行代码。我在下面放了一个简化的例子,只是为了说明这个想法。有没有人知道这样做的语法,或者是否有完全更好的方法?
//实例
doSomething(5,10,functionA,functionB );
函数doSomething(x,y,myFunctionA,myFunctionB){
if(x> y){
myFunctionA();
} else {
myFunctionB();
}
功能函数A(){
//一些代码
}
> function functionB(){
//一些代码
}
I want a function to take another function as a parameter, and execute
it as a line of code. I put a simplified example below, just to
illustrate the idea. Does anyone know the syntax to do this, or is there
perhaps a better approach entirely?
// EXAMPLE
doSomething(5, 10, functionA, functionB);
function doSomething(x, y, myFunctionA, myFunctionB) {
if (x > y) {
myFunctionA();
} else {
myFunctionB();
}
}
function functionA() {
// some code
}
function functionB() {
// some code
}
:)你知道你已经有了解决方案吗?这是将函数引用传递给另一个函数的正确
语法。
:) did you know you already got the solution? That is the correct
syntax for passing a function reference to another function.
dx27s在10/25 /上表示以下内容2005 3:52 PM:
dx27s said the following on 10/25/2005 3:52 PM:
我想要一个函数将另一个函数作为参数,并将其作为一行代码执行。我在下面放了一个简化的例子,只是为了说明这个想法。有没有人知道这样做的语法,或者是否有完全更好的方法?
//实例
doSomething(5,10,functionA,functionB功能doSomething(x,y,myFunctionA,myFunctionB){
if(x> y){
myFunctionA();
I want a function to take another function as a parameter, and execute
it as a line of code. I put a simplified example below, just to
illustrate the idea. Does anyone know the syntax to do this, or is there
perhaps a better approach entirely?
// EXAMPLE
doSomething(5, 10, functionA, functionB);
function doSomething(x, y, myFunctionA, myFunctionB) {
if (x > y) {
myFunctionA();
窗口[myFunctionA]();
-
兰迪
comp.lang。 javascript常见问题解答 - http://jibbering.com/faq &新闻组周刊
window[myFunctionA]();
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
这篇关于使用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!