Peter said:
David Mark wrote:
Sorry, I have confused matters here. However, I would like to point
out that it does not make sense to test against
Function.prototype.call, be it in the negative direct or the positive
indirect way. That the object has a(n *external*) `call' property has
nothing to do with whether or not it is callable (with an argument
list), having an *internal* [[Call]] method.
It is not meant to return true for anything but Functions. It is not an
isCallable function.
I would expect that the primary purpose of an isFunction function would
be to see if something is callable. I can't imagine a practical
scenario where I'd want to know if something is a function without
wanting to know whether it's callable.
Given that the proposed function returns false for host objects in IE,
and true for the same objects in (all?) other browsers, it doesn't seem
worth recommending.
Since it seems that the focus should be on whether something is callable
and not whether or not it is, strictly, a Function, then I'd suggest
that isCallable is what is required, not isFunction.
If you are prepared to accept that isFunction(document.getElementById)
returns false even if it is a function and callable.
So it is being used as if it were isCallable.
I know a callable host object may not be a "function" but if I was
using a function called "isFunction" I'd expect that if I send it any
callable object it would return true.
Either that or there was a prominently placed isCallable function that
also pointed out the difference between the two, something like:
"isFunction returns true if an object is a function, but does
not guarantee that it can be called."
"To test if an object is callable, use isCallable. It returns
true if an object is callable, regardless of whether it is a
function or not."
I agree (at least for now). I think the repository should gain
foundational functions like this as needed for bigger pieces of code
(e.g. Ajax, events, etc).
There seems to be agreement that determining whether an object is a
function is not a reliable indicator of whether it may be called, and
that there is a general need to determine whether or not something is
callable. Therefore, there is a need for an isCallable function.
Such a function could be used for feature testing host functions and
also native functions where appropriate.
In an ECMAScript ed3 compliant implementation, typeof should reliably
indicate whether something is callable (since the very definition of
when typeof obj returns 'function' is that the object implements
[[call]]) then all that needs to be dealt with are edge cases, i.e. IE.
I think it is a mistake to look for public properties called 'call', as
that may not indicate that it is a method. Such a function could be
based on:
var isCallable = (function(){
if (typeof document.getElementById == 'function') {
return function(o) {
return typeof o == 'function';
}
}
if (typeof document.getElementById == 'object') {
return function(o){
// What test works in IE for host functions?
// The following seems too loose:
return typeof o == 'function'
|| typeof o == 'object';
}
}
})();
However clearly there is a need for a better test for IE host functions.