_.each(list, iterator, [context]) 中的上下文是什么?
我是 underscore.js 的新手。 _.each()
中的 [context]
的用途是什么?应该如何使用呢?
I am new to underscore.js. What is the purpose of [context]
in _.each()
? How should it be used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
context 参数只是设置迭代器函数中
this
的值。工作示例: http://jsfiddle.net/a6Rx4/
它使用正在迭代的数组中每个成员的编号,以获取
someOtherArray
索引处的项目,该索引由this
表示,因为我们将其作为上下文参数传递。如果您不设置上下文,则
this
将引用window
对象。附加:
回答
这样做的好处是什么?为什么不直接引用 someOtherArray[num] 而不是 this[num]?
在下面的评论中发现了已投票的问题,让我们将匿名iteratee
回调移动到一个函数中以便于重用:您可以看到
this
引用如何允许我们在具有不同context< 的多个
_.each
调用中重用iteratee
函数/代码>值。如果我们将someOtherArray
硬编码在iteratee
中,那么这将不起作用。The context parameter just sets the value of
this
in the iterator function.Working Example: http://jsfiddle.net/a6Rx4/
It uses the number from each member of the Array being iterated to get the item at that index of
someOtherArray
, which is represented bythis
since we passed it as the context parameter.If you do not set the context, then
this
will refer to thewindow
object.Extras:
To answer the
What's the advantage of that? Why not just refer to someOtherArray[num] rather than this[num]?
upvoted question found in the comments below, let's move the anonymousiteratee
callback into a function for easy re-use:You can see how the
this
reference allows us to re-use theiteratee
function across multiple_.each
calls with differentcontext
values. This would not work if we had thesomeOtherArray
hardcoded inside theiteratee
.context
是this
在迭代器函数中引用的位置。例如:context
is wherethis
refers to in your iterator function. For example:上下文允许您在调用时提供参数,从而可以轻松自定义通用的预构建帮助程序函数。
一些示例:
即使从有限的示例中,您也可以看到“额外参数”对于创建可重用代码有多么强大。您通常可以采用低级助手,而不是为每种情况创建不同的回调函数。目标是让您的自定义逻辑以最少的样板捆绑一个动词和两个名词。
诚然,箭头函数消除了许多泛型纯函数的“代码高尔夫球”优势,但语义和一致性优势仍然存在。
我总是向帮助程序添加“use strict”,以在传递基元时提供本机
[].map()
兼容性。否则,它们会被强制转换为对象,这通常仍然有效,但特定于类型更快更安全。The context lets you provide arguments at call-time, allowing easy customization of generic pre-built helper functions.
some examples:
Even from the limited examples, you can see how powerful an "extra argument" can be for creating re-usable code. Instead of making a different callback function for each situation, you can usually adapt a low-level helper. The goal is to have your custom logic bundling a verb and two nouns, with minimal boilerplate.
Admittedly, arrow functions have eliminated a lot of the "code golf" advantages of generic pure functions, but the semantic and consistency advantages remain.
I always add
"use strict"
to helpers to provide native[].map()
compatibility when passing primitives. Otherwise, they are coerced into objects, which usually still works, but it's faster and safer to be type-specific._.each 的简单使用
这是可以使用
_.each
的简单示例:输出:
使用下划线,而不是多次调用
addItem
:您可以这样 与使用这些项目连续调用
addItem
三次相同。基本上,它会迭代您的数组,并为每个项目调用调用x.addItem(item)
的匿名回调函数。匿名回调函数与addItem
成员函数类似(例如,它需要一个项目)并且毫无意义。因此,最好_.each
避免这种间接调用并直接调用addItem
,而不是通过匿名函数:但这不起作用,因为在篮子的
addItem 内部
成员函数this
不会引用您创建的x
篮子。这就是为什么您可以选择传递购物篮x
以用作[context]
:使用 _.each 和 context 的完整示例:
简而言之,如果您以任何方式传递给
_.each
的回调函数使用this
那么您需要指定this
应该引用什么在你的回调函数中。在我的示例中,x
似乎是多余的,但x.addItem
只是一个函数,可能与x
或完全无关>basket
或任何其他对象,例如:换句话说,您可以在回调中将一些值绑定到
this
,或者您也可以使用 bind 直接像这样:一般来说,如果某个
underscorejs
方法采用回调函数,并且您希望在某个对象的某些成员函数上调用该回调(例如使用this
的函数),那么您可以将该函数绑定到某个对象或将该对象作为[context]
参数传递,这就是主要目的。在 underscorejs 文档的顶部,这正是他们所说的: 如果传递了一个,则 iteratee 会绑定到上下文对象< /a>Simple use of _.each
Here's simple example that could use
_.each
:Output:
Instead of calling
addItem
multiple times you could use underscore this way:which is identical to calling
addItem
three times sequentially with these items. Basically it iterates your array and for each item calls your anonymous callback function that callsx.addItem(item)
. The anonymous callback function is similar toaddItem
member function (e.g. it takes an item) and is kind of pointless. So, instead of going through anonymous function it's better that_.each
avoids this indirection and callsaddItem
directly:but this won't work, as inside basket's
addItem
member functionthis
won't refer to yourx
basket that you created. That's why you have an option to pass your basketx
to be used as[context]
:Full example that uses _.each and context:
In short, if callback function that you pass to
_.each
in any way usesthis
then you need to specify whatthis
should be referring to inside your callback function. It may seem likex
is redundant in my example, butx.addItem
is just a function and could be totally unrelated tox
orbasket
or any other object, for example:In other words, you bind some value to
this
inside your callback, or you may as well use bind directly like this:In general, if some
underscorejs
method takes a callback function and if you want that callback be called on some member function of some object (e.g. a function that usesthis
) then you may bind that function to some object or pass that object as the[context]
parameter and that's the primary intention. And at the top of underscorejs documentation, that's exactly what they state: The iteratee is bound to the context object, if one is passed正如其他答案中所解释的,
context
是要在传递给each
的回调中使用的this
上下文。我将借助 underscore 源代码 中相关方法的源代码来解释这
一点
_.each
或_.forEach
的内容如下:这里需要注意第二条语句
这里,
context
被传递给另一个方法optimizeCb
及其返回的函数然后分配给稍后调用的iteratee
。从上面optimizeCb的方法定义可以看出,如果没有传递context,则原样返回func。如果传递
context
,则调用回调函数,因为使用
call()
用于通过设置this
上下文来调用方法它的。因此,当在func
中使用this
时,它将引用context
。您可以将
context
视为forEach
在 JavaScript 中。As explained in other answers,
context
is thethis
context to be used inside callback passed toeach
.I'll explain this with the help of source code of relevant methods from underscore source code
The definition of
_.each
or_.forEach
is as follows:Second statement is important to note here
Here,
context
is passed to another methodoptimizeCb
and the returned function from it is then assigned toiteratee
which is called later.As can be seen from the above method definition of
optimizeCb
, ifcontext
is not passed thenfunc
is returned as it is. Ifcontext
is passed, callback function is called asfunc
is called withcall()
which is used to invoke a method by settingthis
context of it. So, whenthis
is used insidefunc
, it'll refer tocontext
.You can consider
context
as the last optional parameter toforEach
in JavaScript.