isF

Is argument a function?

by dbjdbj

JavaScript

/*
'bomb' if planted will make isF() below fail.
But. What is the likelyhood to meet it in real life situations?
*/
var bomb = { 
    toString : undefined, valueOf: function(o) {return "function BOMBA!"; }
} ;

/* 
V.5    --- almost perfect ?
of course it can be slightly optimized .
*/
function isF ( f ) { 
    if (Object.prototype.toString.call(f).match("String") == "String" ) return false ;
   try {  return /^\s*\bfunction\b/.test(f) ;
   } catch (x) {   return false ;
   }
}

/* fails on 'bomb' planted
   but not on anything else
 */
console.dir(bomb)
console.log( "isF() on above returns: " + isF(bomb) ) ;