What is the meaning of "callback.call( value, i, value )" in jQuery's each method? What is the meaning of "callback.call( value, i, value )" in jQuery's each method? jquery jquery

What is the meaning of "callback.call( value, i, value )" in jQuery's each method?


The call method exists on all functions in Javascript. It allows you to call the function and in doing so set the value of this within that function.

function myFunc() {    console.log(this);}myFunc.call(document.body);

In this example, this within myFunc will be document.body.

The first parameter of call is the value to be set as this; subsequent parameters are passed on to the function as normal parameters. So, in your example:

callback.call( value, i, value )

this is equivalent to

callback(i, value)

except that, within the callback, this is now also set to value.


The .each() method calls the callback you pass it with the element (current iteration "target") as both the context object (the value of this) and as the second parameter.

Thus, in one of those functions:

$('.foo').each(function(i, elem) {  var $this = $(this), $elem = $(elem);

The variables $this and $elem are interchangeable.

The first argument to .call() is the value to which this should be bound, if that wasn't clear. The rest of the arguments to .call() are just passed as plain arguments to the function.


This calls the callback method with this set to value (the first parameter to call) and with the arguments i and value. (The other parameters to call)