我有这个代码块,我发现特别冗长且难以理解:调用堆栈充满了隐含的函数,并且隐含地添加了参数。换句话说,我想通过将每个中调用的函数与每个函数分开来澄清我的代码。
看那个例子:
$(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 ?
答案 0 :(得分:4)
尝试传递函数参考
<强> Live Demo 强>
$(xml).find('group').each(myfun);
function myfun(i, item)
{
alert(item.id);
}
答案 1 :(得分:2)
您也可以这样做:
$(xml).find('group').each(function(){
yourFunction();
});