检查变量是否为函数类型

假设我有任何变量,定义如下:

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

我想要一个检查变量类型是否像函数一样的函数。即:

function foo(v) {if (v is function type?) {/* do something */}};
foo(a);

如何检查变量a是否为Function上述定义的类型

LSam2020/03/10 10:21:50

you should use typeOf operator in js.

var a=function(){
    alert("fun a");
}
alert(typeof a);// alerts "function"
Tom阳光达蒙2020/03/10 10:21:50

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
凯LEY2020/03/10 10:21:50

I found that when testing native browser functions in IE8, using toString, instanceof, and typeof did not work. Here is a method that works fine in IE8 (as far as I know):

function isFn(f){
    return !!(f && f.call && f.apply);
}
//Returns true in IE7/8
isFn(document.getElementById);

Alternatively, you can check for native functions using:

"getElementById" in document

Though, I have read somewhere that this will not always work in IE7 and below.

逆天Eva2020/03/10 10:21:50

If you use Lodash you can do it with _.isFunction.

_.isFunction(function(){});
// => true

_.isFunction(/abc/);
// => false

_.isFunction(true);
// => false

_.isFunction(null);
// => false

This method returns true if value is a function, else false.

Gil小哥伽罗2020/03/10 10:21:50

Something with more browser support and also include async functions could be:

const isFunction = value => value && (Object.prototype.toString.call(value) === "[object Function]" || "function" === typeof value || value instanceof Function);

and then test it like:

isFunction(isFunction); //true
isFunction(function(){}); //true
isFunction(()=> {}); //true
isFunction(()=> {return 1}); //true
isFunction(async function asyncFunction(){}); //true
isFunction(Array); //true
isFunction(Date); //true
isFunction(Object); //true
isFunction(Number); //true
isFunction(String); //true
isFunction(Symbol); //true
isFunction({}); //false
isFunction([]); //false
isFunction("function"); //false
isFunction(true); //false
isFunction(1); //false
isFunction("Alireza Dezfoolian"); //false
米亚十三Harry2020/03/10 10:21:50

An other simply way:

var fn = function () {}
if (fn.constructor === Function) {
  // true
} else {
  // false
}
小卤蛋村村2020/03/10 10:21:50

Try the instanceof operator: it seems that all functions inherit from the Function class:

// Test data
var f1 = function () { alert("test"); }
var o1 = { Name: "Object_1" };
F_est = function () { };
var o2 = new F_est();

// Results
alert(f1 instanceof Function); // true
alert(o1 instanceof Function); // false
alert(o2 instanceof Function); // false
村村樱2020/03/10 10:21:50

var foo = function(){};
if (typeof foo === "function") {
  alert("is function")
}

番长千羽2020/03/10 10:21:50

Underscore.js使用更精细但性能更高的测试:

_.isFunction = function(obj) {
  return !!(obj && obj.constructor && obj.call && obj.apply);
};

请参阅:http//jsperf.com/alternative-isfunction-implementations

编辑:更新的测试表明typeof可能更快,请参阅http://jsperf.com/alternative-isfunction-implementations/4

梅西里2020/03/10 10:21:50
if (typeof v === "function") {
    // do something
}