美文网首页
判断一个变量是否是函数

判断一个变量是否是函数

作者: Allan要做活神仙 | 来源:发表于2019-03-15 10:03 被阅读0次

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"

相关文章

网友评论

      本文标题:判断一个变量是否是函数

      本文链接:https://www.haomeiwen.com/subject/uigkmqtx.html