[ACCEPTED]-What is the meaning of "callback.call( value, i, value )" in jQuery's each method?-functional-programming

Accepted answer
Score: 50

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

function myFunc() {


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

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

callback.call( value, i, value )

this is equivalent to

callback(i, value)

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

Score: 1

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

Thus, in one of 6 those functions:

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

The variables $this and $elem are 5 interchangeable.

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

Score: 1

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

More Related questions