javascript 如何在不使用原型的情况下链接函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9338439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to chain functions without using prototype?
提问by mithril333221
I have a bunch of useful functions that I have collected during my whole life.
我有一堆有用的功能,是我一生收集的。
function one(num){
return num+1;
}
function two(num){
return num+2;
}
I can call them with two(two(one(5)))
我可以打电话给他们 two(two(one(5)))
But I would prefer to use (5).one().two().two()
但我更愿意使用 (5).one().two().two()
How can I achieve this without using prototype?
如何在不使用原型的情况下实现这一目标?
I tried to see how underscore chain works, but their code is too intense to understand it
我试图看看下划线链是如何工作的,但他们的代码太复杂了,无法理解
回答by Bergi
The dot syntax is reserved for objects. So you can do something like
点语法是为对象保留的。所以你可以做类似的事情
function MyNumber(n) {
var internal = Number(n);
this.one = function() {
internal += 1;
// here comes the magic that allows chaining:
return this;
}
// this.two analogous
this.valueOf = function() {
return internal;
}
}
new MyNumber(5).one().two().two().valueOf(); // 10
Or you're going to implement these methods on the prototype of the native Number object/function. That would allow (5).one()...
或者您将在本机 Number 对象/函数的原型上实现这些方法。那将允许(5).one()...
回答by mithril333221
In order to avoid having to call toValue
at the end of the chain as in @Bergi's solution, you can use a function with attached methods. JS will call toValue
automatically when trying to convert to it a primitive type.
为了避免toValue
像@Bergi 的解决方案那样在链的末尾调用,您可以使用带有附加方法的函数。JS 会toValue
在尝试将其转换为原始类型时自动调用。
function MyNumber(n) {
function x () { }
x.one = function() { n++; return this; };
x.valueOf = function() { return n; };
return x;
}
Then,
然后,
MyNumber(5).one().one()
> 7
回答by hugomg
A nice and general alternative is creating a custom function composition function
一个不错的通用替代方法是创建自定义函数组合函数
var go = function(x, fs){
for(var i=0; i < fs.length; i++){
x = fs[i](x);
}
return x;
}
You can call it like this:
你可以这样称呼它:
go(5, [one, two, two])
I am personaly not a big fan of method chaining since it restricts you to a predefined set of functions and there is kind of an impedance mismatch between values inside the "chaining object" and free values outside.
我个人不是方法链接的忠实粉丝,因为它限制了你使用一组预定义的函数,并且“链接对象”内部的值和外部的自由值之间存在某种阻抗不匹配。
回答by br2000
Another alternative is to use lodash flowfunction. For example:
另一种选择是使用lodash 流函数。例如:
var five = _.flow(one, two, two)
five(5)
I prefer assigning a new chain to a variable. It gives it a clear name and encourages re-use.
我更喜欢将新链分配给变量。它给了它一个清晰的名称并鼓励重复使用。
Btw, lodash also helps in passing additional arguments to the functions of the chain. For example:
顺便说一句,lodash 还有助于将额外的参数传递给链的函数。例如:
var addFive = _.flow(
_.partialRight(_.add, 1),
_.partialRight(_.add, 2),
_.partialRight(_.add, 2)
)
There are many other useful functions to help in functional chaining, e.g., partial, spread, flip, negate, etc.
还有许多其他有用的函数可以帮助进行函数链接,例如partial、spread、flip、negate等。