Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The solution is the binding of variables through closure.</p> <hr> <p>As a more basic example, here is an example function that receives and calls a callback function, as well as an example callback function:</p> <pre><code>function callbackReceiver(callback) { callback("Hello World"); } function callback(value1, value2) { console.log(value1, value2); } </code></pre> <p>This calls the callback and supplies a single argument. Now you want to supply an additional argument, so you wrap the callback in closure.</p> <pre><code>callbackReceiver(callback); // "Hello World", undefined callbackReceiver(function(value) { callback(value, "Foo Bar"); // "Hello World", "Foo Bar" }); </code></pre> <p>Or, more simply using <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions" rel="noreferrer">ES6 Arrow Functions</a>:</p> <pre><code>callbackReceiver(value =&gt; callback(value, "Foo Bar")); // "Hello World", "Foo Bar" </code></pre> <hr> <p>As for your specific example, I haven't used the <code>.post</code> function in jQuery, but a quick scan of the documentation suggests the call back should be a <i>function pointer</i> with the following signature:</p> <pre><code>function callBack(data, textStatus, jqXHR) {}; </code></pre> <p>Therefore I think the solution is as follows:</p> <pre><code>var doSomething = function(extraStuff) { return function(data, textStatus, jqXHR) { // do something with extraStuff }; }; var clicked = function() { var extraStuff = { myParam1: 'foo', myParam2: 'bar' }; // an object / whatever extra params you wish to pass. $.post("someurl.php", someData, doSomething(extraStuff), "json"); }; </code></pre> <p>What is happening?</p> <p>In the last line, <code>doSomething(extraStuff)</code> is <i>invoked</i> and the result of that invocation is a <i>function pointer</i>.</p> <p>Because <code>extraStuff</code> is passed as an argument to <code>doSomething</code> it is within scope of the <code>doSomething</code> function.</p> <p>When <code>extraStuff</code> is referenced in the returned anonymous inner function of <code>doSomething</code> it is bound by closure to the outer function's <code>extraStuff</code> argument. This is true even after <code>doSomething</code> has returned.</p> <p>I haven't tested the above, but I've written very similar code in the last 24 hours and it works as I've described.</p> <p>You can of course pass multiple variables instead of a single 'extraStuff' object depending on your personal preference/coding standards.</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