How to get 'typeof' of the value of a variable

This question already has an answer here:

  • How to execute a JavaScript function when I have its name as a string 29 answers

  • Supposing that your function is global you can verify if it exists using window object (the object where are stored the global variables).

     if (typeof window[functionName] === 'function') {
         // function exists
     }
    

    JSFIDDLE


    You can use window[functionName] as follows:

    function Result(){}
    var functionName = 'Result';
    if (typeof window[functionName] == 'function'){
        alert("function exists, don't load it");
              }else{
        alert("function does not exist, load it");
    }
    

    However you need to make sure you escape the single quote in don't or use double quotes since it terminates your String literal.

    Here is a working example: http://jsfiddle.net/WN92K/


    你可以这样做,看看你的功能是全球性的。

    if (window[functionName] instanceof Function){
    
    链接地址: http://www.djcxy.com/p/94838.html

    上一篇: JQuery检查变量是否是函数

    下一篇: 如何获得变量值的'typeof'