current position:Home>Determine whether a function exists in JavaScript - function_ exists

Determine whether a function exists in JavaScript - function_ exists

2022-06-24 09:12:23Rudon coastal fishing village

Code

// JS Determine whether a function exists  ( Function will not be run )
function function_exists (fname = '') {
    let res = false;

    try {
        if(typeof(eval(fname))=="function") {
            //  There is 
            res = true;
        } else {
            //  non-existent 
            res = false;
        }
    } catch(e) {
        //  non-existent 
        res = false;
    }

    return res;
}

To match try catch Talent

in addition , When testing , Will not actually run the specified function

effect

 

thank

JavaScript How to judge functions in 、 Does the variable exist -js course -PHP Chinese net JavaScript Judgment function in 、 The method to determine whether a variable exists :1、 Determine whether the specified function exists , The code is 【if (typeof(eval(funcName)) == "function")】;2、 Determine whether the specified variable exists .https://www.php.cn/js-tutorial-473375.html

copyright notice
author[Rudon coastal fishing village],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/175/202206240742112460.html

Random recommended