"how to test for functions" should be in the FAQ, yes?

D

David Golightly

<FAQENTRY>

Weird. I go to Google and search for "javascript faq". These are the
top results:

http://www.javascripter.net/faq/ind...cript/script.htmhttp://www.jibbering.com/faq/

I've got what I assume is an FAQ question:

"How can I check to see if a function has been defined?"

I searched on those pages for stuff like "define" and "function" and
"test" and didn't find my question.

To answer your question, "functions" aren't special in this case - you
need a general ability to test if an identifier is defined:

if (typeof possibleFn == 'function') {
// do stuff
}

gives you what you want; in general you can use

if (typeof possibleFn != 'undefined') {
// do stuff
}

for all your needs, unless you suspect possibleFn may be assigned a
non-function value somewhere, in which case the former (typeof
(possibleFn) == 'function') is what you want.

I don't know why the comp.lang.javascript FAQ is ranked 3rd in the
Google results. It's probably a matter of those other sites putting
greater importance on SEO.

Also, I doubt that this specific question will find its way into the
FAQ, unless under the guise of the general case (checking any
identifier's status); there are infinite questions that beginners ask,
and once they've read the FAQ, it's reasonable to come here to find
answers to stuff that's not there.

-David
 
R

Richard Cornford

David said:
On Sep 21, 4:20 pm, Jake Barnes wrote:
To answer your question, "functions" aren't special in
this case - you need a general ability to test if an
identifier is defined:

if (typeof possibleFn == 'function') {
// do stuff
}

gives you what you want; in general you can use

if (typeof possibleFn != 'undefined') {
// do stuff
}
Also, I doubt that this specific question will find its way
into the FAQ, unless under the guise of the general case
(checking any identifier's status);
<snip>

And the general answer is already in the FAQ, in the resources linked to
from 4.26.

Richard.
 
J

Jake Barnes

<snip>

And the general answer is already in the FAQ, in the resources linked to
from 4.26.


Hmm, at jibbering, 4.26 is this:

4.26 How do I detect Opera/Netscape/IE?

http://www.jibbering.com/faq/#FAQ4_26


I don't see numbers for http://www.javascripter.net/faq/index.htm

At irt.org, 426 points at this:

http://www.irt.org/script/426.htm

Q426 How can I change the mouse pointer to an hourGlass prior to long
running scripts, then reset after the script complete?

So I don't see what you're suggesting. None of these 3 FAQs answer my
question, but I think my question, and the answer, should be in an
FAQ.
 
J

Jake Barnes

To answer your question, "functions" aren't special in this case - you
need a general ability to test if an identifier is defined:

if (typeof possibleFn == 'function') {
// do stuff

}

gives you what you want; in general you can use

if (typeof possibleFn != 'undefined') {
// do stuff

}

for all your needs, unless you suspect possibleFn may be assigned a
non-function value somewhere, in which case the former (typeof
(possibleFn) == 'function') is what you want.

I don't know why the comp.lang.javascript FAQ is ranked 3rd in the
Google results. It's probably a matter of those other sites putting
greater importance on SEO.

Also, I doubt that this specific question will find its way into the
FAQ, unless under the guise of the general case (checking any
identifier's status); there are infinite questions that beginners ask,
and once they've read the FAQ, it's reasonable to come here to find
answers to stuff that's not there.

You don't feel this question is asked frequently? I had the
impression it was in the category of "common".
 
R

Richard Cornford

Jake said:
On Sep 21, 9:15 pm, Richard Cornford wrote:


Hmm, at jibbering, 4.26 is this:

4.26 How do I detect Opera/Netscape/IE?

http://www.jibbering.com/faq/#FAQ4_26


I don't see numbers for http://www.javascripter.net/faq/index.htm
<snip>

FAQs other than the FAQ for the comp.lang.javascript newsgroup are of
limited relevance to discussions of the comp.lang.javascript FAQ and its
contents.
So I don't see what you're suggesting. None of these 3
FAQs answer my question, but I think my question, and the
answer, should be in an FAQ.

If your problem is an inability/unwillingness to read what is written
modifying the content of written documents will not help you.

Richard.
 
R

RobG

Hmm, at jibbering, 4.26 is this:

4.26 How do I detect Opera/Netscape/IE?

Where it shows a simple way to determine if a method is available:

"if (document.getElementById && ..."


The linked resources go into more detail. If you are testing for a
defined function or method the above is OK, but if you are looking for
a value, or want to be more specific in the test, use typeof, or
hasOwnProperty or whatever more specific test you think is
appropriate.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top