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?
You need to call the
toStringmethod directly from theObject.prototypeobject:jQuery has a local variable named
toStringthat refers to the method onObject.prototype.Calling just
toString.call(obj);without declaring atoStringidentifier 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
callandapplymethods.Those methods allow you to invoke a function passing the first argument as the
thisvalue of the invoked function.On ECMAScript 3, if this argument was
undefinedornull, thethisvalue 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.toStringto throw an exception, because an object argument was expected.The specification of that method changed, now if the
thisvalue refers toundefinedornullthe 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,undefinedandnullare 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
typeofoperator?Implementation bugs, for example in Chrome/Safari/WebKit, the
typeoffor RegExp objects returns"function", because RegExp objects where made callable, e.g.:The
typeofoperator 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
execmethod.