Check if a variable is of function type

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

    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.

提交回复
热议问题