Check if a variable is of function type

后端 未结 18 1649
北海茫月
北海茫月 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:38

    I think you can just define a flag on the Function prototype and check if the instance you want to test inherited that

    define a flag:

    Function.prototype.isFunction = true; 
    

    and then check if it exist

    var foo = function(){};
    foo.isFunction; // will return true
    

    The downside is that another prototype can define the same flag and then it's worthless, but if you have full control over the included modules it is the easiest way

提交回复
热议问题