输入banner图图片脚本导航/分类

JavaScript通过字符串调用函数的实现方法_javascript技巧

本文实例讲述了JavaScript通过字符串调用函数的实现方法。分享给大家供大家参考。具体分析如下:

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;
}