Check if a variable is of function type

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

    The below seems to work for me as well (tested from node.js):

    var isFunction = function(o) {
         return Function.prototype.isPrototypeOf(o);
    };
    
    console.log(isFunction(function(){})); // true
    console.log(isFunction({})); // false
    

提交回复
热议问题