PDA

View Full Version : Is Ext.isFunction completely reliable?



WBX
23 Jan 2013, 10:43 PM
I had posted this question on the doc page before I noticed an admonition not to post questions about code or usage so I'm repeating it here:

In "Secrets of the JavaScript Ninja", the following caveat is given to their implementation of isFunction which uses a technique similar to the one Ext.isFunction uses:

"There is one notable exception, however. (Isn’t there always?) Internet Explorer reports methods of DOM elements with a type of “object,” like so: typeof domNode.getAttribute == "object" and typeof inputElem.focus == "object". So this particular technique doesn’t cover this case."


I don't see where this exception is accounted for in the Ext.isFunction code. Am I missing something or does this caveat apply here as well?

Thanks

mitchellsimoens
28 Jan 2013, 12:35 PM
I haven't had any issues but usually don't have to check if something is a function. Here is what the isFunction looks like:


isFunction:
// Safari 3.x and 4.x returns 'function' for typeof <NodeList>, hence we need to fall back to using
// Object.prototype.toString (slower)
(typeof document !== 'undefined' && typeof document.getElementsByTagName('body') === 'function') ? function(value) {
return toString.call(value) === '[object Function]';
} : function(value) {
return typeof value === 'function';
}

WBX
28 Jan 2013, 4:43 PM
Thanks for the response Mitchell.


I had already looked at the code as I noted in my post.


I was actually wondering if the following caveat quoted from "Secrets of the JavaScript Ninja" regarding their implementation of isFunction is addressed in Ext.isFunction:


"There is one notable exception, however. (Isn’t there always?) Internet Explorer reports methods of DOM elements with a type of “object,” like so: typeof domNode.getAttribute == "object" and typeof inputElem.focus == "object". So this particular technique doesn’t cover this case."


I don't see from looking at the code where it is addressed.

skirtle
28 Jan 2013, 8:50 PM
I looked into this before. If I remember correctly it can't identify those IE functions as functions. To the best of my knowledge there is no safe and reliable way to detect all such functions.

Also, it should be noted that for many of them they don't behave exactly like functions. While they are invokable they don't have all the other properties you might expect a normal function to have. So to some extent it depends on your definition of a function as to whether they should even count.

In practice I've never seen this cause a real application any problems.

WBX
28 Jan 2013, 9:20 PM
Thanks skirtle. I hear what you and Mitchell are saying about not having encountered problems with this. I still think that it should be noted in the documentation for completeness and also so people like me aren't posing pesky questions like this. ;)

I'll go ahead and post another comment on the doc page linking back to this post to see if someone will be kind enough to update it.

Thanks again.