如何在函数调用中传递参数?

时间:2010-05-10 18:03:35

标签: javascript

function rp_marcadesmarcaFarm(valor) {

    FM_log(3, "marcadesmarcaFarm called = "+valor);

    for (i = 0; i < farmList.length; i++) {

        var arr        = farmList[i].split("|");
        var xy         = arr[0].split(",");
        var fvillageId = xy2id(parseInt(xy[0]), parseInt(xy[1]));

        GM_setValue("farmAtivada_"+suffixLocal+fvillageId, valor);
        GM_setValue("farmAtivada_"+suffixLocal+i, valor);
    };

    reloadFarmTable();
};

function createLinkButton(text, title, jsFunction, value) {   

  var button       = dom.cn("a");    
  button.href      = "javascript:void(0)";    
  button.innerHTML = text;    
  button.title     = title;

  if (jsFunction != null) {    
    button.addEventListener('click', jsFunction, false);    
  }

  return button;    
}

createLinkButton("X", T('CHECKFARM_M'), rp_marcadesmarcaFarm(true));

显然,调用createLinkBut​​ton时的最后一个参数(rp_marcadesmarcaFarm(true))不起作用。如果我改为:

createLinkButton("X", T('CHECKFARM_M'), rp_marcadesmarcaFarm);

它有效。那么如何将(true)变量传递给createLinkBut​​ton的第三个参数?

2 个答案:

答案 0 :(得分:5)

createLinkButton("X", T('CHECKFARM_M'), function() {rp_marcadesmarcaFarm(true)});

答案 1 :(得分:1)

createLinkButton正在等待一个函数,但rp_marcadesmarcaFarm(true)调用该函数并实际传递返回值。

你想要的是一个匿名函数,它会调用rp_marcadesmarcaFarm(true)并将其传递给createLinkButton

createLinkButton("X", T('CHECKFARM_M'), function(){ rp_marcadesmarcaFarm(true); });