JavaScript中我们可以把根据函数名的字符串来调用函数,这样我们就可以实现动态函数调用,只需要传递一个函数的名字即可调用该函数。
代码如下:var strFun = "someFunction"; //Name of the function to be called
var strParam = "this is the parameter"; //Parameters to be passed in function
//Create the function
var fn = window[strFun];
//Call the function
fn(strParam);
下面是一个详细的调用实例
代码如下:
function fnFooBar(strVal) {
alert(strVal);
return 1;
}
希望本文所述对大家的javascript程序设计有所帮助。