How can I check if a javascript variable is functi

2018-12-31 19:48发布

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?

标签: javascript
17条回答
零度萤火
2楼-- · 2018-12-31 20:19

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.

查看更多
十年一品温如言
3楼-- · 2018-12-31 20:20

Try instanceof: 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
查看更多
墨雨无痕
4楼-- · 2018-12-31 20:21

The solution as some previous answers has shown is to use typeof. the following is a code snippet In NodeJs,

    function startx() {
      console.log("startx function called.");
    }

 var fct= {};
 fct["/startx"] = startx;

if (typeof fct[/startx] === 'function') { //check if function then execute it
    fct[/startx]();
  }
查看更多
ら面具成の殇う
5楼-- · 2018-12-31 20:23

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
查看更多
谁念西风独自凉
6楼-- · 2018-12-31 20:25

There are several ways so I will summarize them all

  1. Best way is:
    
    function foo(v) {if (v instanceof Function) {/* do something */} };
    
    
    Most performant (no string comparison) and elegant solution - the instanceof operator has been supported in browsers for a very long time, so don't worry - it will work in IE 6.
  2. Next best way is:
    
    function foo(v) {if (typeof v === "function") {/* do something */} };
    
    
    disadvantage of typeof is that it is susceptible to silent failure, bad, so if you have a typo (e.g. "finction") - in this case the `if` will just return false and you won't know you have an error until later in your code
  3. The next best way is:
    
    function isFunction(functionToCheck) {
        var getType = {};
        return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
    }
    
    
    This has no advantage over solution #1 or #2 but is a lot less readable. An improved version of this is
    
    function isFunction(x) {
        return Object.prototype.toString.call(x) == '[object Function]';
    }
    
    
    but still lot less semantic than solution #1
查看更多
伤终究还是伤i
7楼-- · 2018-12-31 20:27

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

查看更多
登录 后发表回答