Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Javascript functions that check if a function exists.</strong></p> <p>With <a href="http://api.jquery.com/jQuery.isFunction/" rel="nofollow noreferrer">jQuery.isFunction()</a> you may test a parameter to check if it is (a) defined and (b) is of type "function." Since you asked for jQuery, this function will tickle your fancy. </p> <pre><code>jQuery.isFunction(YourFunction) </code></pre> <p>If you wish not to use jQuery for whatever reason, here's a barebones function based on code from <a href="http://www.idealog.us/2007/02/check_if_a_java.html" rel="nofollow noreferrer">Idealog</a> that will check if the variable is of type <code>function</code>.</p> <pre><code>function isFunction(fn){ return typeof fn === 'function' } </code></pre> <p>Sometimes you already know it's a function and for the sake of optimization find no reason to recheck it's type, in this case, here's function that simply checks if the <code>variable</code> [possibly a function] is defined</p> <pre><code>function isDefined(foo){ return foo !== 'undefined' } </code></pre> <p><strong>How to use these functions</strong></p> <p>Using jQuery:</p> <pre><code>function foo(){} if(jQuery.isFunction(foo)) alert('This is a function'); </code></pre> <p>With either of the <em>non-jQuery</em> Javascript functions provided above. Depending on the context of usage, these functions may, or may not be reliable. See more below</p> <pre><code>function foo(){} if(isFunction(foo)) alert('is of type function'); if(isDefined(foo)) alert('if this is a function, it is defined'); </code></pre> <p>Check both undefined and using <code>jQuery.isFunction</code> </p> <pre><code>if (typeof myfunc !== 'undefined' &amp;&amp; $.isFunction(myfunc)) { //do something } </code></pre> <p><a href="https://stackoverflow.com/questions/20027715/jquery-isfunction-check-error-function-is-not-defined/20027726#20027726">Source</a></p> <p><strong>Is jQuery.isFunction() superior?</strong> </p> <p>According to <em><a href="http://kflorence.myopenid.com/" rel="nofollow noreferrer">Kyle Florence</a></em> <code>jQuery.isFunction()</code> it could superior in some situations. Particularly useful in some edge cases when using jQuery methods, see his <a href="http://api.jquery.com/jQuery.isFunction/#comment-128339529" rel="nofollow noreferrer">explanation</a>. </p> <blockquote> <p>In certain situations in some browsers, things are incorrectly returned as the "function" type, or things that are in fact functions are returned as another type. There are several test cases you can see here: <a href="https://github.com/jquery/jque" rel="nofollow noreferrer">https://github.com/jquery/jque</a>...</p> <p>One example:</p> <p>var obj = document.createElement("object");</p> <p>// Firefox says this is a function typeof obj; // => "function"</p> <p>Keep in mind these are mostly edge cases, but the reason $.isFunction was made was simply to be positive about something being a function (which can be quite important for the jQuery library itself, maybe not so much for your code).</p> </blockquote> <p>Thanks <strong><a href="https://stackoverflow.com/users/113716/patrick-dw">patrick dw</a></strong> for pointing out Kyles Article. (Patrick DW deleted his account)</p> <p>From <a href="http://api.jquery.com/jquery.isfunction/#entry-longdesc" rel="nofollow noreferrer">jQuery.com</a></p> <blockquote> <p>Note: As of jQuery 1.3, functions provided by the browser like alert() and DOM element methods like getAttribute() are not guaranteed to be detected as functions in browsers such as Internet Explorer.</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload