Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The way you call <code>ev</code>, <code>this</code> will refer to the global object (<code>window</code> in browsers). <a href="https://developer.mozilla.org/en/JavaScript/Reference/Operators/this" rel="nofollow">MDN has a nice summary about <code>this</code>.</a></p> <p>You have to explicitly set <code>this</code>, using <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call" rel="nofollow"><code>call</code> <em><sup>[MDN]</sup></em></a> or <a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/apply" rel="nofollow"><code>apply</code> <em><sup>[MDN]</sup></em></a>, or just pass the callback to <a href="http://api.jquery.com/each/" rel="nofollow"><code>each</code> <em><sup>[docs]</sup></em></a>:</p> <pre><code>$.fn.test = function( ev ) { return this.each(ev); }; </code></pre> <p>By using <code>each</code>, you make sure that <code>this</code> inside the callback you pass only refers to <strong>one</strong> DOM element, just like other callbacks work, like the one you pass to <code>.click()</code>.</p> <p>Otherwise, inside a plugin method, <code>this</code> refers to <em>all selected elements</em>. So this would be <strong>wrong</strong> (or at least different from typical jQuery callbacks):</p> <pre><code>$.fn.test = function( ev ) { ev.call(this); return this; }; </code></pre> <p>On the other side, this would be OK as well, but is unnecessarily verbose:</p> <pre><code>$.fn.test = function( ev ) { return this.each(function() { ev.call(this); }); }; </code></pre> <p>Though you have to do this if you wanted to pass custom arguments to your callback (<code>ev</code>), otherwise <code>ev</code> receives the same arguments as the <code>each</code> callback.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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