I am new to underscore.js. What is the purpose of [context]
in _.each()
? How should it be used?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
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 passedcontext
is wherethis
refers to in your iterator function. For example: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.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.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.