Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you use Dojo you just call dojo.hitch() that does almost exactly what you want. Almost &mdash; because it can be used to pack the context as well. But your example is first:</p> <pre><code>dojo.hitch(out, "hello")("world"); dojo.hitch(out, "hello", "world")(); </code></pre> <p>As well as:</p> <pre><code>var A = { sep: ", ", out: function(a, b){ console.log(a + this.sep + b); } }; // using functions in context dojo.hitch(A, A.out, "hello")("world"); dojo.hitch(A, A.out, "hello", "world")(); // using names in context dojo.hitch(A, "out", "hello")("world"); dojo.hitch(A, "out", "hello", "world")(); </code></pre> <p>dojo.hitch() is the part of the Dojo Base, so as soon as you included dojo.js it is there for you.</p> <p>Another general facility is available in dojox.lang.functional.curry module (documented in <a href="http://lazutkin.com/blog/2008/jan/12/functional-fun-javascript-dojo/" rel="nofollow noreferrer">Functional fun in JavaScript with Dojo</a> &mdash; just look on this page for "curry"). Specifically you may want to look at curry(), and partial().</p> <p>curry() accumulates arguments (like in your example) but with one difference: as soon as the arity is satisfied it calls the function returning the value. Implementing your example:</p> <pre><code>df.curry(out)("hello")("world"); df.curry(out)("hello", "world"); </code></pre> <p>Notice that the last line doesn't have "()" at the end &mdash; it is called automatically.</p> <p>partial() allows to replace arguments at random:</p> <pre><code>df.partial(out, df.arg, "world")("hello"); </code></pre>
 

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