当前位置: 代码迷 >> 综合 >> isFunction
  详细解决方案

isFunction

热度:5   发布时间:2024-01-16 15:34:14.0
[size=xx-large]isFunction[/size]

[size=large]标准写法是:[/size]

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


[size=large]深层次:[/size]
[list]
[*]有博客说, 之前的chrome会识别 new Regexp()为function, 没有测试之前的chrome, 现在的不会了
[*]有博客说, document.write在IE下会识别为object, 其实新的写法, 依然还是object, 不信,jquery测试下, 同理getAttribute也是一样的
[*]jquery的isFunction注释
[code]
// 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";
}
[/code]
[*]jquery中的 type(obj), 即是 Object.prototype.toString.call(obj)
[*]jquery已经不支持DOM方法,以及像alert这样的方法, 因为他们在IE都返回false,无法区分
[*]#2968 见 [url]http://bugs.jquery.com/ticket/2968[/url]
[/list]


[size=large]最简单的写法:[/size]

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


[*] 这个写法, 在jquery的isFunction测试用例中, 全部通过 [url]https://code.google.com/p/jqueryjs/source/browse/trunk/jquery/test/unit/core.js[/url]
[*] 简单的就是最好的~~


[size=large]测试用例 (修改自jquery的isFunction测试用例)[/size]

<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 ? '√':'×') + '   ' + 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>

  相关解决方案