Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
106
rated 0 times [  110] [ 4]  / answers: 1 / hits: 26000  / 14 Years ago, sun, october 31, 2010, 12:00:00

each() method in jQuery contains such a statement:



callback.call( value, i, value ) 


I couldn't understand what this statement means exactly.



I know what callback and call mean but I couldn't get the arguments of the function call: (value,i,value). What does this mean?



The statement is used in a for block of each() but my question is independent of that context.



from the jQuery source:



for ( var value = object[0];
i < length &&
callback.call( value, i, value ) // <=== LOOK!
!== false;
value = object[++i] ) {}

More From » jquery

 Answers
38

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.


[#95123] Thursday, October 28, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
daquanmilesw

Total Points: 57
Total Questions: 102
Total Answers: 110

Location: Wallis and Futuna
Member since Sat, Aug 6, 2022
2 Years ago
daquanmilesw questions
;