isFunction() or isObject(), that is the question ?

In my previous post, we have established few simple rules and presented a simple mechanism for dealing with object “roles” in JavaScript. In the same time we have consluded that isFunction() and isObject() will not be that “trivial” to implement in IE.

Here is the simple requirement: write a function that will return true if argument given is a function. Hint: In IE, typeof window.alert , returns “object”

Here is my portable isFunction() :

It works all the time and everywhere. Everybody enjoy? How about passing it this :

Well … above version does not work that well. Here is the (hopefully) proper version :

Is this little “hack” breakable ? Well it is, if given this :

Oh well , how about this new version then :

Is this it then? The wholy grail of javascript? An universal isFunction() ?
No it is not .. yet. Above (x + "")will fail if this is given as an argument to isFunction(x) :

So, here we go again, with yet another version :

Of course here I am concerned primarily with the solution.
For the above optimizations are certainly possible and are left as an “excersize to the reader” ;)

At last…?

Wait there is more … Verion 5. Seems slightly ridiculous, but it also seems to work (almost) perfectly.

See the comment bellow explaining the single case which brakes V.5. In any case, we have a solution for determining if object is a function (aka “callable”). which leaves yet another “tough cooky” for IE “side” : isObject(). Which will return trues or false , if argument given is object or not. So in the IE this must be false :

With the help of isFunction() we can build isObject(), for IE, that will also work properly. We will simly check first if argument is a function. If it is it can not be object in the same time. Here is the working code :

Update 2012 Oct 08
Please consider this code released under the MIT licence.

NOTE: this post was also inspired with:

7 thoughts on “isFunction() or isObject(), that is the question ?”

Comments are closed.