Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If we use this code:
JavaScript
function StringCon(arg) {
  return function (ar) {
   return arg + ar;

}
}
StringCon("hello")(" world")//-> "hello world"

Is there a way to create a function that would create that closure automatically and we could pass as many function calls as we want?
For example, what if I wanted to add "!" to the end of my string
StringCon("hello")(" world")("!")

I would need to create another function call manually, is it possible to add it pro grammatically?

What I have tried:

I am aware about existence of "arguments" property but can I group these function calls together and add them?
Posted
Updated 31-Jan-17 6:22am

1 solution

I don't know how it works. But the underscore library has a chain function.
This can chain an endless list of functions, but won't end the closure automatically.

You need to call the result() function.

But you could look a their code to see how they did it.

Underscore.js[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900