javascript `_.forEach` 和 `$.each` 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13824221/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 19:52:52  来源:igfitidea点击:

The difference between `_.forEach` and `$.each`

javascriptjqueryloopsforeachunderscore.js

提问by Lorraine Bernard

These two functions, _.forEachand $.each, taken respectively from underscoreand jQueryseems to make the same thing.

这两个函数, _.forEach$.each,分别取自underscorejQuery似乎做了同样的事情。

What are the possible reasons to prefer one implementation to the other?

偏爱一种实现而不是另一种实现的可能原因是什么?

采纳答案by antonjs

_.forEachand $.eachdiffers for the arguments passed to the callback.

_.forEach$.each针对不同的参数传递给回调。

If you use _.forEach, the first argument passed to the callback is the value, not the key.
So if you don't bother at all about the key you should use _.forEach.

如果使用_.forEach,则传递给回调的第一个参数是值,而不是键。
因此,如果您根本不关心密钥,则应该使用_.forEach.

Other differences:

其他区别:

  1. _.forEachit will be a bit faster as it uses the native Arrray.prototype.forEachin modern browsers.
  2. the this value is also different. jQuery's behavior doesn't follow the standard, underscore's does.
  1. _.forEach它会快一点,因为它Arrray.prototype.forEach在现代浏览器中使用本机。
  2. 这个值也不同。jQuery 的行为不遵循标准,下划线的行为。

回答by Kos

jQuerylooks like:

jQuery看起来像:

something.each( function(index, Element) )

Underscorelooks like:

下划线看起来像:

_.each(list, function(Element, index, list), [context])
// or
_(list).each(function(Element, index, list), [context])

Native array.forEachlooks like:

本机 array.forEach看起来像:

array.forEach(function(Element, index, list), [context])

So:

所以:

  • Underscore keeps the same argument order as native forEach
  • There are two differences between JQuery's and Underscore's implementation:
    • JQuery sets thisto Element, native and Underscore allow you to provide your own context
    • Native and underscore also provide the list itself as third argument to the callback.
  • 下划线与本机保持相同的参数顺序 forEach
  • JQuery 和 Underscore 的实现有两个区别:
    • JQuery 设置thisElement, native 和 Underscore 允许您提供自己的上下文
    • 本机和下划线还提供列表本身作为回调的第三个参数。


Edit: Why is it useful to be able to set the context?

编辑:为什么能够设置上下文有用?

Consider that you have some kind of object:

考虑您有某种对象:

var worker = new FooWorker();
worker.process(something);
worker.process(somethingElse);

Say you want to call that method on every value from an array.
Using the context parameter, you can simply say:

假设您想对数组中的每个值调用该方法。
使用上下文参数,您可以简单地说:

myArray.forEach(worker.process, worker);

while without it you'd need to be more verbose (and do one more function call per element):

如果没有它,您将需要更详细(并且每个元素再执行一次函数调用):

// native
myArray.forEach( function(i, e) {worker.process(e);} );
// jquery
$(myArray).each( function() {worker.process(this); } );

This is one situation where JQuery's foreaching convention makes things less convenient.

这是 JQuery 的 foreaching 约定使事情变得不那么方便的一种情况。

回答by Denys Séguret

Both of them mainly provide a replacement for the forEachfunction that isn't available in IE8.

两者主要是替代IE8中没有的forEach函数。

They don't add much if you're iterating over arrays.

如果您迭代数组,它们不会增加太多。

The main difference, apart the order of the callback arguments, is that in jQuery the value is also available as context of the callback's call (this is why the value, less important, is only the second argument provided). This isn't really a major reason to prefer it unless you really like to avoid passing an argument to the function :

除了回调参数的顺序之外,主要区别在于,在 jQuery 中,该值也可用作回调调用的上下文(这就是为什么该值不太重要,仅提供第二个参数)。除非您真的想避免将参数传递给函数,否则这并不是真正喜欢它的主要原因:

var product = 1;
$.each([1, 2, 3], function(){ product *= this });

Most often, you don't use both libraries, so you simply use the iterating function provided by the library you have.

大多数情况下,您不会同时使用这两个库,因此您只需使用您拥有的库提供的迭代函数。

If you happen to import both libraries, I would suggest to use the underscore function as

如果您碰巧导入了这两个库,我建议您使用下划线函数作为

  • it's the most similar to the standard ECMAScript function on arrays so you'll more easily migrate the day IE8 will be dead
  • it's more efficient as it uses the native function when it's available :
  • 它与数组上的标准 ECMAScript 函数最相似,因此在 IE8 将死的那一天,您将更容易迁移
  • 它更有效,因为它在可用时使用本机函数:

See source:

见来源

...
if (nativeForEach && obj.forEach === nativeForEach) {
     obj.forEach(iterator, context);
...