Check if a variable is of function type

后端 未结 18 1724
北海茫月
北海茫月 2020-11-22 15:37

Suppose I have any variable, which is defined as follows:

var a = function() {/* Statements */};

I want a function which checks if the type

18条回答
  •  忘掉有多难
    2020-11-22 16:21

    Sure underscore's way is more efficient, but the best way to check, when efficiency isn't an issue, is written on underscore's page linked by @Paul Rosania.

    Inspired by underscore, the final isFunction function is as follows:

    function isFunction(functionToCheck) {
     return functionToCheck && {}.toString.call(functionToCheck) === '[object Function]';
    }
    

提交回复
热议问题