javascript 链接 Underscore.js 函数的首选方式是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9626512/
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
What is the preferred way of chaining Underscore.js functions?
提问by Horatio Alderaan
Underscore.js has two ways of calling functions, which I will refer to as object-style and function-style. Object-style looks like the following:
Underscore.js 有两种调用函数的方式,我将其称为对象样式和函数样式。对象样式如下所示:
_(myObject).each(function (val, key) {
console.log(key, val);
});
Function-style, on the other hand, looks like this:
另一方面,函数风格看起来像这样:
_.each(myObject, function (val, key) {
console.log(key, val);
});
I was happily using object-style calls in my code, but at some point, however, the object style of call disappeared from the underscore.js documentation (though object-style calls still work perfectly fine). I've also seen hints around the place (like in the backbone.js documentation) that the function-style is 'better' or 'preferred'.
我很高兴在我的代码中使用对象样式调用,但是在某些时候,对象样式调用从 underscore.js 文档中消失了(尽管对象样式调用仍然可以正常工作)。我还看到了一些提示(例如在backbone.js 文档中),函数样式是“更好”或“首选”。
So, is the function-style of call the preferred method? And if so, can someone explain the reasoning behind this?
那么,调用的函数风格是首选方法吗?如果是这样,有人可以解释这背后的原因吗?
Update:@ggozad has partially answered my question. But it seems my understanding of how underscore.js works was formed way back around version 0.4.2. Reading through the change history for underscore.js, you can see this entry for version 1.2.4:
更新:@ggozad 部分回答了我的问题。但我对 underscore.js 如何工作的理解似乎是在 0.4.2 版左右形成的。阅读 underscore.js 的更改历史记录,您可以看到版本 1.2.4 的以下条目:
You now can (and probably should) write
_.chain(list)
instead of_(list).chain()
.
您现在可以(并且可能应该)编写
_.chain(list)
而不是_(list).chain()
.
I would like to know why you should write _.chain(list)
instead of _(list).chain()
.
我想知道为什么你应该写_.chain(list)
而不是_(list).chain()
.
回答by Russell Davis
The answer by @ggozad is actually very misleading. The object-oriented style has nothing to do with chaining. The example given:
@ggozad 的回答实际上非常具有误导性。面向对象的风格与链接无关。给出的例子:
_([1,2,3]).map(function (item) { return item * 2; }).map(function (item) { return item*3;});
is actually not using underscore chaining at all! It only works because the built-in JS array object has it's own map() function. Try a function that's not built-in (like shuffle) and you'll see it breaks:
实际上根本没有使用下划线链接!它之所以有效,是因为内置的 JS 数组对象有它自己的 map() 函数。尝试一个非内置函数(如 shuffle),你会看到它中断了:
_([1,2,3]).shuffle().shuffle();
The only way to get underscore chaining is to call chain()
, which you can do using either style (OO or functional).
获得下划线链接的唯一方法是调用chain()
,您可以使用任何一种样式(OO 或函数式)进行调用。
As for why the documentation says you should use _.chain
, I'm guessing it's just a style preference. I've opened an issue at GitHubfor clarification.
至于为什么文档说你应该使用_.chain
,我猜这只是一种风格偏好。我已经在 GitHub 上打开了一个问题以进行澄清。
回答by ggozad
When _
is used as a function it essentially wraps the argument. The wrapper provides all the normalunderscore functions.
当_
用作函数时,它基本上包装了参数。包装器提供了所有正常的下划线函数。
The difference it makes apart from style is that using the OOP style (or object style in your definition) is that it produces chainable wrapped objects. So it's easy to do:
它与样式的区别在于,使用 OOP 样式(或定义中的对象样式)是它生成可链接的包装对象。所以很容易做到:
_([1,2,3]).map(function (item) { return item * 2; }).map(function (item) { return item*3;});
The equivalent would be:
相当于:
_.map(_.map([1,2,3], function (item) { return item * 2 }), function (item) { return item * 3 });
which is probably less clear.
这可能不太清楚。