调用带有很多参数的函数会消耗更多的内存吗?

问题描述

| 我在寻找可以处理网站缩略图的传呼机,但没有找到任何内容,因此必须自己编写。我写了一个函数,可以根据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,$(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,\"n\"); }).appendTo($pager); // prev,next,first,last buttons
    $(\'<span class=\"mod\">Prev</span>\').click(function(){ addPager(s,\"p\"); }).prependTo($pager);   
    $(\'<span class=\"mod\">Last</span>\').click(function(){ addPager(s,\"l\"); }).appendTo($pager);    
    $(\'<span class=\"mod\">First</span>\').click(function(){ addPager(s,\"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,(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,(i-1));}} // if current prev goto prev
    else if (current == \"f\") { addPager(s,2); } // 1st
    else if (current == \"l\") { addPager(s,(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许可,如果需要,可以使用它,它可以正常工作:) 当我多次调用
addPager(param,param,param)
时,它具有如此多的参数...它会占用大量内存还是类似的东西? 如何使该功能更好或更快速? 为了便于理解,我发表了一些评论,但是我认为这不是一个复杂的功能。     

解决方法

最好创建一个带有属性的对象,然后传递该对象而不是多个参数。对象的每个属性将代表一个参数。您可以将其视为选项/配置对象。 然后,您可以将对象作为单个参数传递给函数,然后使用对象的属性。 这是一种更好的方法,因为从长远来看,它提供了更好的可伸缩性。 但是,我不确定使用许多参数会产生开销。在我看来,它只是很难阅读且扩展性不强。