问题
I have this code block I find particularly long and hard to udnerstood : the call stack is full of implicit functions and paramters implicitely added to it. in other words, i would like to clarify my code by separating the function called in the each from the each itself.
Look that example :
$(xml).find('group').each(function () {
var groupName = $(this).attr('name');
// There is here around 100 lines of codes I would like to split in
// at least five functions, And I'm sure it is possible to use named functions
// instead of implicit ones, no ?
回答1:
Try passing function reference
Live Demo
$(xml).find('group').each(myfun);
function myfun(i, item)
{
alert(item.id);
}
回答2:
You could also just do:
$(xml).find('group').each(function(){
yourFunction();
});
来源:https://stackoverflow.com/questions/16086769/when-using-jquery-each-is-it-possible-to-use-a-non-anonymous-function