欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript通過字符串調(diào)用函數(shù)的實現(xiàn)方法

 更新時間:2015年03月18日 15:17:23   作者:上大王  
這篇文章主要介紹了JavaScript通過字符串調(diào)用函數(shù)的實現(xiàn)方法,實例分析了javascript動態(tài)調(diào)用函數(shù)的技巧,需要的朋友可以參考下

本文實例講述了JavaScript通過字符串調(diào)用函數(shù)的實現(xiàn)方法。分享給大家供大家參考。具體分析如下:

JavaScript中我們可以把根據(jù)函數(shù)名的字符串來調(diào)用函數(shù),這樣我們就可以實現(xiàn)動態(tài)函數(shù)調(diào)用,只需要傳遞一個函數(shù)的名字即可調(diào)用該函數(shù)。

復制代碼 代碼如下:
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);

 
下面是一個詳細的調(diào)用實例
復制代碼 代碼如下:
<input type="text" id="functionName" name="functionName" size="20" value="fnFooBar">
    <input type="text" id="functionParam" name="functionParam" size="30" value="Happy New Year.!!">
    <input type="button" style="font-weight:bold" value="Call" onclick="javascript:call();">
    <br>
    <pre>
    function fnFooBar(strVal) {
            alert(strVal);
            return 1;
        }
   </pre>
<br>
<script>
function fnFooBar(strVal) {
    alert(strVal);
    return 1;
}
function call() {
    var strFunctionName = document.getElementById("functionName").value;
    var strFunctionParam = document.getElementById("functionParam").value;
    var fn = window[strFunctionName]
    var ret = fn(strFunctionParam);
}
</script>

希望本文所述對大家的javascript程序設(shè)計有所幫助。

相關(guān)文章

最新評論