How to check if a function is already defined?

后端 未结 10 1362
后悔当初
后悔当初 2021-01-01 08:52

How to check if a function is already defined ?

相关标签:
10条回答
  • 2021-01-01 09:45
    function test(){}
    
    if(typeof test  != "undefined")
        // function is allready defined
    
    0 讨论(0)
  • 2021-01-01 09:47

    typeof return string,so you can use the JavaScript typeof operator to find the type of a JavaScript variable.

    if ( typeof(youerFunctionName) === 'undefined' )
    {
        console.log('undefined');
    }
    
    0 讨论(0)
  • 2021-01-01 09:49

    Like so:

    if (typeof myFunc != 'undefined') {
        // Assign myFunc
    }
    

    Don't just test it against undefined, which is not a constant and can be reassigned.

    0 讨论(0)
  • 2021-01-01 09:49

    Say your function is called func:

    if (func) {
      // Function is already defined (or at least something is defined there)
    } else {
      // Function is not defined
    }
    
    0 讨论(0)
提交回复
热议问题