Testing whether something is a function
Hi,
The jquery.js file for jQuery 1.2.1 contains the following curious
code and comment:
// This may seem like some crazy code, but trust me when I say that this
// is the only cross-browser way to do this. --John
isFunction: function( fn ) {
return !!fn && typeof fn != "string" && !fn.nodeName &&
fn.constructor != Array && /function/i.test( fn + "" );
},
I expect this will sound like terribly naive, but why doesn't
typeof fn === 'function'
work?
And doesn't the code above give a false positive if fn is a
non-function, non-string, non-array object without a nodeName property
whose toString() happens to include the string "function"?
--
Text by me above is hereby placed in the public domain
Cheers,
--MarkM