jQuery 1.4 源码 449 行(core.js 431 行),判断是否为函数的方法如下(思路来源于 Douglas Crockford 的《The Miller Device》):
isFunction: function( obj ) {
return toString.call(obj) === "[object Function]";
},
同时 jQuery 的作者也作了部分注释:
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).
即:此方法在 IE 下无法正确识别 DOM 方法和一些函数(例如 alert 方法等)。
为什么会这样呢?
详细看测试页面:http://www.planabc.net/demo/isfunction/
会发现在 IE 下用 typeof 检测 alert、confirm 方法以及 DOM 的方法显示 object,而其他浏览器下显示 function。
那如何完善这个问题呢?
- typeof 检测某个方法(例如:document.getElementById) 是否是 object,如何是,则重写 isFunction 函数;
- 怎样重写呢?正则判断传入的对象字符串后(”" + fn),是否起始位置含有 function,即:/^\s*\bfunction\b/.test(” + fn)。
OK,看下根据以上思路修改后的 isFunction 函数:
var isFunction = (function() {
// Performance optimization: Lazy Function Definition
return "object" === typeof document.getElementById ?
isFunction = function(fn){
try {
return /^\s*\bfunction\b/.test("" + fn);
} catch (x) {
return false
}
}:
isFunction = function(fn){
return "[object Function]" === Object.prototype.toString.call(fn);
};
})()
参考阅读:
witter:
共有8 条评论
顶师父,不错的方法,可惜IE还是无法正确处理这种极端情况:
var obj = {toString:void 0,valueOf:function(){return ‘function xx’;}};
alert(isFunction(obj));
如果排除这种极端情况,其实所有浏览器都是支持return /^\s*\bfunction\b/.test(“” + fn);的,又何必分开呢?
@渔隐
你再测试一下,你所说的例子,应该可以处理的,因为正则里用的是^\s*,避免了这些极端的情况。再则,正则的效率相对还是没有原来的方式效率高的。又见惰性函数!…
return /^\s*\bfunction\b/.test(“” + fn); 这里有个bug很明显:fn是函数指针,”" + fn实际会执行”"+fn.toString();如果之前有人故意把fn.toString重载了,则该判断必然失效。
测试如下:
function test(){
alert(“just test”);
}
//重载toString
test.toString = function(){
};
alert((“” + test) === “undefined”);//true
@dongyuwei 谢谢,的确还有问题。
test = {}
test.toString = function(){
return ‘function’;
}
alert(/^\s*\bfunction\b/.test(” + test));
哇,看到我头都晕了,看来真的要自我增值了。。。
经测试,alert等window方法和getElementById等dom方法在ie下的toString为undefind。
在加上 渔隐 所提醒的valueOf判断,应该可以修复bug