Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One of the most common uses of function wrappers is to maintain or lock in that function's context. For example, if you have some object and you want to use one of it's method's as the <code>onclick</code> handler for a given element:</p> <pre><code>someElement.onclick = someObject.someMethod; </code></pre> <p>If <code>someObject.someMethod</code> makes any references to <code>this</code>, instead of <code>this</code> pointing to <code>someObject</code> it will instead point to <code>someElement</code> because the context has changed. By wrapping the <code>someObject.someMethod</code></p> <pre><code>someElement.onclick = function() { someObject.someMethod() }; </code></pre> <p>you are still executing <code>someMethod</code> as a method of <code>someObject</code> rather than as a method of <code>someElement</code>.</p> <p>However, if the event handler method never makes any references to <code>this</code>, then a wrapper isn't needed.</p> <p>From the example code that you posted, if you just did </p> <pre><code>$(callingImg)[0].setAttribute("onclick", removeChildren(this)); </code></pre> <p><code>removeChildren(this)</code> would be executed immediately and it's return value would be assigned as the <code>onclick</code> handler.</p> <hr> <p>Here's some sample code to illustrate what's happening</p> <pre><code>var FakeElement = function() { this.name = "FakeElement"; this.onclick = function() {}; }; var FakeEventHandler = function() { this.name = "FakeHandlerObject"; this.clickHandler = function() { console.log("`this` = ", this.name); }; }; var e = new FakeElement(); var h = new FakeEventHandler(); // Normal usage, `this` points to instance of `h` console.info("h.clickHandler();"); h.clickHandler(); // Context of this is changed to `e` instead of `h` console.info("e.onclick = h.clickHandler;"); e.onclick = h.clickHandler; e.onclick(); // Wrapped to maintain proper context of `this` within `h` console.info("e.onclick = function() { h.clickHandler(); };"); e.onclick = function() { h.clickHandler(); }; e.onclick(); // Executed immediately and returns `null` causing an error in `e.onclick();` console.info("e.onclick = h.clickHandler();"); e.onclick = h.clickHandler(); e.onclick(); </code></pre> <p>Output:</p> <pre><code>h.clickHandler(); `this` = FakeHandlerObject e.onclick = h.clickHandler; `this` = FakeElement e.onclick = function() { h.clickHandler(); }; `this` = FakeHandlerObject e.onclick = h.clickHandler(); `this` = FakeHandlerObject </code></pre> <hr> <p>As a side note, it looks like you're using jQuery. If that's the case, you can simply do </p> <pre><code>$($(callingImg)[0]).click(function() { removeChildren(this); }); </code></pre> <p>better still, if there is always one <code>callingImg</code> or you want to apply the same click handler to every <code>callingImg</code>, you can just do </p> <pre><code>$(callingImg).click(function() { removeChildren(this); }); </code></pre>
    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