isFunction (Code Worth Recommending Project)

D

David Mark

David, then why isn't the function that simple?

The original discussion "When is a function not a function" concerned
a function of mine that could receive a Function or Object object as a
callback parameter. The specifics were that it would call a
predetermined method on Object objects. If a callable host object
(typeof(o) == 'function') is passed, which would be a violation of the
rules, it should not call it and therefore fail silently. This posted
version is the solution for that. It will not mistake callable host
objects (or builtin functions) for suitable callback Functions. It
will fail instantly if passed an inappropriate callback parameter.

Is it of any practical use? Not for me at this time as I don't handle
callbacks that way anymore. It did spark a lot of discussion in the
original thread though.

[snip]
I can see this point. I think David is suggesting he always knows that
the 'o' variable is at least declared and so the call to the wrapper
function will not error.

Exactly.
 
P

Peter Michaux

[snip]
1. For host methods (aka feature detection), use:
if (hostObj && hostObj.method) {
hostObj.method();
}

I consider that insufficient. For one, it will error on methods of
ActiveX objects.
[snip]

This is a partial solution to feature testing host methods. This is
what I use:

var reFeaturedMethod = new RegExp('^function|object$', 'i');

var isFeaturedMethod = function(o, m) {
var t = typeof(o[m]);
return !!((reFeaturedMethod.test(t) && o[m]) || t == 'unknown');

};

That covers ActiveX methods and null objects. As Thomas pointed out
(repeatedly), o must be a full qualified reference, which seems to be
an issue for him, though I don't consider it a problem.

Is there a particular need to involve a regular expression?

When would "reFeaturedMethod.test(t)" be true but o[m] would be falsy?

Which situation does the "t=='unknown'" cover? It seems odd that if
typeof(o[m]) is "unknown" then I should believe that o[m] is a
callable method.

I think I'm catching up on this stuff.
 
P

Peter Michaux

[snip]


David, then why isn't the function that simple?

The original discussion "When is a function not a function" concerned
a function of mine that could receive a Function or Object object as a
callback parameter. The specifics were that it would call a
predetermined method on Object objects. If a callable host object
(typeof(o) == 'function') is passed, which would be a violation of the
rules, it should not call it and therefore fail silently. This posted
version is the solution for that. It will not mistake callable host
objects (or builtin functions) for suitable callback Functions. It
will fail instantly if passed an inappropriate callback parameter.

So your callback was actually being called as

callback.call(obj, arg1, arg2)

and you really needed to insure that there was a .call() property?

Thanks
 
D

David Mark

[snip]
typeof(o) == 'function'
David, then why isn't the function that simple?
The original discussion "When is a function not a function" concerned
a function of mine that could receive a Function or Object object as a
callback parameter. The specifics were that it would call a
predetermined method on Object objects. If a callable host object
(typeof(o) == 'function') is passed, which would be a violation of the
rules, it should not call it and therefore fail silently. This posted
version is the solution for that. It will not mistake callable host
objects (or builtin functions) for suitable callback Functions. It
will fail instantly if passed an inappropriate callback parameter.

So your callback was actually being called as

callback.call(obj, arg1, arg2)

and you really needed to insure that there was a .call() property?

No. The test of .call (when possible) excludes callable host objects
and builtin functions. So for example, if document.images were passed
(has a [[call]] method in some browsers), it would not be mistaken for
a Function.
 
D

David Mark

[snip]
1. For host methods (aka feature detection), use:
if (hostObj && hostObj.method) {
hostObj.method();
}
I consider that insufficient. For one, it will error on methods of
ActiveX objects.
[snip]

This is a partial solution to feature testing host methods. This is
what I use:
var reFeaturedMethod = new RegExp('^function|object$', 'i');
var isFeaturedMethod = function(o, m) {
var t = typeof(o[m]);
return !!((reFeaturedMethod.test(t) && o[m]) || t == 'unknown');

That covers ActiveX methods and null objects. As Thomas pointed out
(repeatedly), o must be a full qualified reference, which seems to be
an issue for him, though I don't consider it a problem.

Is there a particular need to involve a regular expression?

When would "reFeaturedMethod.test(t)" be true but o[m] would be falsy?

If o[m] is null.
Which situation does the "t=='unknown'" cover? It seems odd that if
typeof(o[m]) is "unknown" then I should believe that o[m] is a
callable method.

ActiveX methods. "Unknown" may seem an odd type, but is actually
appropriate if you dig into ActiveX (COM) interfaces. That is the LCD
interface that all ActiveX objects implement. You can't tell anything
about the object from that interface, other than it is an ActiveX
object.
 
P

Peter Michaux

1. For host methods (aka feature detection), use:
if (hostObj && hostObj.method) {
hostObj.method();
}
I consider that insufficient. For one, it will error on methods of
ActiveX objects.
This is a partial solution to feature testing host methods. This is
what I use:
var reFeaturedMethod = new RegExp('^function|object$', 'i');
var isFeaturedMethod = function(o, m) {
var t = typeof(o[m]);
return !!((reFeaturedMethod.test(t) && o[m]) || t == 'unknown');
};
That covers ActiveX methods and null objects. As Thomas pointed out
(repeatedly), o must be a full qualified reference, which seems to be
an issue for him, though I don't consider it a problem.
Is there a particular need to involve a regular expression?
When would "reFeaturedMethod.test(t)" be true but o[m] would be falsy?

If o[m] is null.

Ahh, yes. JavaScript is a really weird language :)

It is interesting that it is easy to get by quite well without really
thinking about these odd little details on a daily basis.

Which situation does the "t=='unknown'" cover? It seems odd that if
typeof(o[m]) is "unknown" then I should believe that o[m] is a
callable method.

ActiveX methods. "Unknown" may seem an odd type, but is actually
appropriate if you dig into ActiveX (COM) interfaces. That is the LCD
interface that all ActiveX objects implement. You can't tell anything
about the object from that interface, other than it is an ActiveX
object.

As long as "unknown" is the documented ActiveX type and there is not a
known conflict with any other browser then I'm ok with it.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top