If you use Dojo, you simply call dojo.hitch (), which does almost what you want. Nearly; because it can also be used to package context. But your example is first:
dojo.hitch(out, "hello")("world"); dojo.hitch(out, "hello", "world")();
As well as:
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")();
dojo.hitch () is part of the Dojo base, so once you have included dojo.js, it is there for you.
Another publicly available tool is available in the dojox.lang.functional.curry module (registered in Functional Fun in JavaScript with Dojo Just look at this page for "curry"). In particular, you can look at curry () and partial ().
curry () accumulates arguments (as in your example), but with one difference: as soon as legitimacy is executed, it calls a function that returns a value. Implementation of your example:
df.curry(out)("hello")("world"); df.curry(out)("hello", "world");
Note that the last line does not have a "()" at the end - it is called automatically.
partial () allows you to arbitrarily replace arguments:
df.partial(out, df.arg, "world")("hello");