When adapting jQuery's isFunction method
isFunction: function( obj ) {
return toString.call(obj) === "[object Function]";
}
InternetExlorer 8 returns the following error: "Object doesn't support this property or method". A good article digging into this function won't fix this behaviour. To check wether obj is defined, I changed the function in reference to the MSDN article:
isFunction: function( obj ) {
return obj && toString.call(obj) === "[object Function]";
}
Any other ideas for a solution?
I just ran this in IE 8's console:
Works fine.
The blog post you reference concerns IE 9.
What purpose is the
isFunction
function trying to serve? Is it intended to deal with host objects (i.e. DOM nodes,window
etc. in browsers), or regular expressions (which as CMS points out in the comments are callable and return "function" withtypeof
in some browsers)? If not, all you need isIf it is supposed to deal with host methods, then it's flawed because there's no obligation that the browser must return "[object Function]" for host methods. In IE there are many callable host properties that won't return "[object Function]" or will throw an error. For example:
... gives "[object Object]" in IE.
You need to call the
toString
method directly from theObject.prototype
object:jQuery has a local variable named
toString
that refers to the method onObject.prototype
.Calling just
toString.call(obj);
without declaring atoString
identifier on scope, works on Firefox, Chrome, etc, just because the Global object inherits fromObject.prototype
, but this is not guaranteed by the spec.The article you link to talks about a change that was introduced in the ECMAScript 5th Edition specification to the
call
andapply
methods.Those methods allow you to invoke a function passing the first argument as the
this
value of the invoked function.On ECMAScript 3, if this argument was
undefined
ornull
, thethis
value of the invoked function will refer to the global object, for example:But that changed in ES5, the value should now be passed without modification and that caused the
Object.prototype.toString
to throw an exception, because an object argument was expected.The specification of that method changed, now if the
this
value refers toundefined
ornull
the string"[object Undefined]"
or"[object Null]"
will be returned, fixing the problem (thing that I don't think is really good, since both results results feel just wrong,undefined
andnull
are not objects, they are primitives... that made me remembertypeof null == 'object'
... Moreover I think it messes up with the concept of the[[Class]]
internal property, anyway...)Now you might wonder why jQuery uses this method to check for a function object, instead of using the
typeof
operator?Implementation bugs, for example in Chrome/Safari/WebKit, the
typeof
for RegExp objects returns"function"
, because RegExp objects where made callable, e.g.:The
typeof
operator returns"function"
if the object implements the[[Call]]
internal property, which made objects to be callable.This was originally introduced by the Mozilla implementations, invoking a RegExp object is equivalent to call the
exec
method.