大海 Online

javascript

isFunction

[size=xx-large]isFunction

标准写法是:

 var isFunction = function(obj) {
       return Object.prototype.toString.call(obj) === '[object Function]'
 }

深层次:

    // See test/unit/core.js for details concerning isFunction.
    // Since version 1.3, DOM methods and functions like alert
    // aren't supported. They return false on IE (#2968).
    isFunction: function( obj ) {
        return jQuery.type(obj) === "function";
    }

最简单的写法:

 var isFunction = function(obj) {
       return typeof obj === 'function'
 }

测试用例 (修改自jquery的isFunction测试用例)

<body>
<ul id="result"></ul>

<script>
var jQuery = {
	isFunction: function(obj) {
		return typeof obj === 'function'
	}
}

function ok(bool, text) {
	document.getElementById('result').innerHTML += ('<li>' + (bool ? '√':'×') + ' &nbsp; ' + text);
}

function test() {
        // Make sure that false values return false
        ok( !jQuery.isFunction(), "No Value" );
        ok( !jQuery.isFunction( null ), "null Value" );
        ok( !jQuery.isFunction( undefined ), "undefined Value" );
        ok( !jQuery.isFunction( "" ), "Empty String Value" );
        ok( !jQuery.isFunction( 0 ), "0 Value" );

        // Check built-ins
        // Safari uses "(Internal Function)"
        ok( jQuery.isFunction(String), "String Function("+String+")" );
        ok( jQuery.isFunction(Array), "Array Function("+Array+")" );
        ok( jQuery.isFunction(Object), "Object Function("+Object+")" );
        ok( jQuery.isFunction(Function), "Function Function("+Function+")" );

        // When stringified, this could be misinterpreted
        var mystr = "function";
        ok( !jQuery.isFunction(mystr), "Function String" );

        // When stringified, this could be misinterpreted
        var myarr = [ "function" ];
        ok( !jQuery.isFunction(myarr), "Function Array" );

        // When stringified, this could be misinterpreted
        var myfunction = { "function": "test" };
        ok( !jQuery.isFunction(myfunction), "Function Object" );

        // Make sure normal functions still work
        var fn = function(){};
        ok( jQuery.isFunction(fn), "Normal Function" );

        var obj = document.createElement("object");

        // Firefox says this is a function
        ok( !jQuery.isFunction(obj), "Object Element" );

        // IE says this is an object
        // Since 1.3, this isn't supported (#2968)
        //ok( jQuery.isFunction(obj.getAttribute), "getAttribute Function" );

        var nodes = document.body.childNodes;

        // Safari says this is a function
        ok( !jQuery.isFunction(nodes), "childNodes Property" );

        var first = document.body.firstChild;

        // Normal elements are reported ok everywhere
        ok( !jQuery.isFunction(first), "A normal DOM Element" );

        var input = document.createElement("input");
        input.type = "text";
        document.body.appendChild( input );

        // IE says this is an object
        // Since 1.3, this isn't supported (#2968)
        //ok( jQuery.isFunction(input.focus), "A default function property" );

        document.body.removeChild( input );

        var a = document.createElement("a");
        a.href = "some-function";
        document.body.appendChild( a );

        // This serializes with the word 'function' in it
        ok( !jQuery.isFunction(a), "Anchor Element" );

        document.body.removeChild( a );

        // Recursive function calls have lengths and array-like properties
        function callme(callback){
                function fn(response){
                        callback(response);
                }

                ok( jQuery.isFunction(fn), "Recursive Function Call" );

                fn({ some: "data" });
        };

        callme(function(){
                callme(function(){});
        });
}

test();

</script>


</body>