判断一个变量是否是函数

2019-03-15  本文已影响0人  Allan要做活神仙

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

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

I want a function which checks if the type of the variable is function-like. i.e. :

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

How can I check if the variable a is of type Function in the way defined above?

One

Object.prototype.toString.call(fn)==='[object Function]'

Two

typeof fn === "function"
上一篇 下一篇

猜你喜欢

热点阅读