snipt

Ctrl+h for KB shortcuts

JavaScript

jQuery test for whether an object has a method?

1
2
3
4
5
6
7
if (!!$.prototype.functionName)

or 

if( $.isFunction( $.fn.someMethod ) ) {
    // it exists
}
https://snipt.net/embed/9fcc24a25431b4fc994b05d8efddb5cf/
https://snipt.net/raw/9fcc24a25431b4fc994b05d8efddb5cf/
9fcc24a25431b4fc994b05d8efddb5cf
js
JavaScript
8
2016-09-17T20:21:02
True
False
False
Jan 31, 2014 at 04:27 AM
/api/public/snipt/109805/
jquery-test-for-whether-an-object-has-a-method
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="k">if</span> <span class="p">(</span><span class="o">!!</span><span class="nx">$</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">functionName</span><span class="p">)</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="nx">or</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="k">if</span><span class="p">(</span> <span class="nx">$</span><span class="p">.</span><span class="nx">isFunction</span><span class="p">(</span> <span class="nx">$</span><span class="p">.</span><span class="nx">fn</span><span class="p">.</span><span class="nx">someMethod</span> <span class="p">)</span> <span class="p">)</span> <span class="p">{</span> </span><span id="L-6"><a name="L-6"></a> <span class="c1">// it exists</span> </span><span id="L-7"><a name="L-7"></a><span class="p">}</span> </span></pre></div> </td></tr></table>
jQuery, method, prototype
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.