javascript - What is context in _.each(list, iterator, [context])? -
i new underscore.js. purpose of [context]
in _.each()
? how should used?
the context parameter sets value of this
in iterator function.
var someotherarray = ["name","patrick","d","w"]; _.each([1, 2, 3], function(num) { // in here, "this" refers same array "someotherarray" alert( this[num] ); // num value array being iterated // this[num] gets item @ "num" index of // someotherarray. }, someotherarray);
working example: http://jsfiddle.net/a6rx4/
it uses number each member of array being iterated item @ index of someotherarray
, represented this
since passed context parameter.
if not set context, this
refer window
object.
Comments
Post a Comment