2011-05-26 96 views
0

我寻找一个寻呼机,我可以处理我的网站的缩略图,但我没有找到任何东西,所以我不得不写我自己的。我写了一个功能,它可以使基于AJAX请求的任何数据寻呼机:使用大量参数调用函数是否消耗更多内存?

function addPager(s, m, f, pager, current) { 
if (!current) {current = 2;} 
// m - element/page 
// s - all elements 
// f - ajax getter function's name like functionName(id, page) 
// current - pressed button's index or p n f l as prev next first last 
if (!pager) { // if the pager not appended 
    if (s%m == 0) { p = s/m; } else {p = parseInt(s/m);} // get the pages number on all elements/elementsonpage 
    $pager = $('<div class="pager clearfix"></div>') // pager holder box 
    for (i=0;i<=p;i++) // append all pages to the pager holder and bind them for click 
    { 
     $('<span>'+(i+1)+'</span>') 
     .bind("click", {page: i}, f) 
     .click(function(){ 
      var ind = $(this).index(); 
      addPager(s, m, f, $(this).parent("div"), ind); // on click call this function again 
     }) 
     .appendTo($pager); // the append 
    } 
    $pager.find("span").eq(0).addClass("on"); // find first element and add on class because its just appended 
    $('<span class="mod">Next</span>').click(function(){ addPager(s, m, f, $(this).parent("div"), "n"); }).appendTo($pager); // prev, next, first, last buttons 
    $('<span class="mod">Prev</span>').click(function(){ addPager(s, m, f, $(this).parent("div"), "p"); }).prependTo($pager); 
    $('<span class="mod">Last</span>').click(function(){ addPager(s, m, f, $(this).parent("div"), "l"); }).appendTo($pager);  
    $('<span class="mod">First</span>').click(function(){ addPager(s, m, f, $(this).parent("div"), "f"); }).prependTo($pager); 
    $pager.children("span").eq(0).addClass("on"); // add on class for the now 1st element (which is the first button) 
    $pager.children("span").eq(1).addClass("disabled"); // disable the prev button 
    $pager.children("span:not(.mod)").each(function(index) { 
     if (((current-3) > (index+2)) || ((current+3) < (index+2))) { $(this).hide(); } else { $(this).show(); } // hide everything but the first 6 buttons (in case of 1000 pages i just show 1 .. 6) 
     if (current == (index+2)) { $(this).addClass("on"); } // add on class to the first element - its probably useless because of the previous addonclass 
     }); 
return($pager); // return it for the function which called. i append in the primary ajax function. 

} 
else { // if the pager is already appended to the site 
    if (s%m == 0) { p = s/m; } // calculate pages 
    else {p = parseInt(s/m);} // calculate pages 

    if (current == "n") {var i = $pager.find("span:not(.mod).on").index(); if ((i>=1) && (i<(p+2))) {addPager(s, m, f, pager, (i+1));}} // if current next goto next 
    else if (current == "p") {var i = $pager.find("span:not(.mod).on").index(); if ((i>2) && (i<=(p+2))) {addPager(s, m, f, pager, (i-1));}} // if current prev goto prev 
    else if (current == "f") { addPager(s, m, f, pager, 2); } // 1st 
    else if (current == "l") { addPager(s, m, f, pager, (p+2)); } // last 
    else { // if any other button pressed 
     $pager = pager; 
     $pager.find("span").removeClass("on disabled"); // remove on or disabled classes on each buttons 
     $pager.find("span:not(.mod)").each(function(index) { // find numbered items only (without the spec buttons like prev or last) 
      if (((current-3) > (index+2)) || ((current+3) < (index+2))) { $(this).hide(); } else { $(this).show(); } // hide all the buttons but the nearest ones 
      if (current == (index+2)) { $(this).addClass("on"); } 
     }); 
     if (current == 2) { $pager.children("span").eq(0).addClass("on"); $pager.children("span").eq(1).addClass("disabled"); }// first button makes the prev diabled and first active 
     if (current == (p+2)) { $pager.children("span").eq(p+4).addClass("on"); $pager.children("span").eq(p+3).addClass("disabled"); } // last button makes the next diabled and last active 
    } 
} 
} 

...这可以这样使用;

$.ajax { 
... 
addPager(allelementsnumber, elementsonpage, thisajaxfunctionsname); 
$pager.appendTo(whereyouwanttoappendthis); 
... 
} 

CC许可,使用它,如果你想,它的做工精细:)

  1. 当我打电话addPager(param, param, param, param, param, param)很多次,这么多参数......不会使用大量的内存或类似的东西?

  2. 如何使此功能更好或更快?

我对简单的理解做了一些评论,但我认为它不是一个复杂的功能。

回答

1

最好是创建一个带有属性的对象,然后通过它来代替多个参数。该对象的每个属性都将表示其中一个参数。你可以把它想象成一个选项/配置对象。

然后您将该对象作为单个参数传递给该函数,然后使用该对象的属性。

这是一个更好的方法,因为它提供了更好的长期可扩展性。

但是,我不确定使用许多参数会产生开销。在我看来,这只是难以阅读而且不具有可扩展性。