可串函数调用javascript [英] Chainable function calls in javascript
本文介绍了可串函数调用javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我们使用此代码:
If we use this code:
function StringCon(arg) {
return function (ar) {
return arg + ar;
}
}
StringCon("hello")(" world")//-> "hello world"
有没有办法创建一个自动创建闭包的函数,我们可以传递任意数量的函数调用? br />
例如,如果我想添加!怎么办?到我的字符串末尾
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")("!")
我需要手动创建另一个函数调用,是否可以通过编程方式添加它?
我尝试过:
我知道存在arguments属性,但是我可以将这些函数调用组合在一起并添加它们吗?
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?
推荐答案
我不知道它是如何工作的。但是下划线库有链函数。
这可以链接无穷无尽的函数列表,但不会自动结束闭包。
你需要调用result()函数。
但你可以查看他们的代码来看看他们是如何做到的。
Underscore.js [ ^ ]
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[^]
这篇关于可串函数调用javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文