循环中的defineSetter

时间:2010-08-24 20:34:37

标签: javascript closures

作为Setting javascript defineGetter and defineSetter in a loop的后续内容,我现在遇到了在循环中定义setter的麻烦。

这是我试图在Greasemonkey脚本中使用的代码。

var setValue_InStoredObject = function(_prefName, _varName, _newValue)
{

  console.info('setValue_InStoredObject:\narguments = ');
  console.info(arguments);

  var tmp_original = JSON.parse(GM_getValue(_prefName, JSON.stringify(defaultColumnPrefixes)));
  console.info(JSON.stringify(tmp_original));

  tmp_original[_varName] = _newValue;
  console.info(JSON.stringify(tmp_original));

  GM_setValue(_prefName,JSON.stringify(tmp_original));

};

var getValue_FromStoredObject = function(_prefName, _varName, _defaultValue) {
  return function() {
    return JSON.parse(GM_getValue(_prefName, JSON.stringify(_defaultValue)))[_varName];
  };
};

function addSettersGetters(_obj, _prefName,_defaults)
{
  console.info(arguments);
  for(var loop_columnName in _defaults)
  {
    var getfunc = getValue_FromStoredObject(_prefName,loop_columnName, _defaults);

    _obj.__defineGetter__(loop_columnName, getfunc);
    var setfunc = function(_newValue) { setValue_InStoredObject(_prefName,loop_columnName, _newValue) };
    _obj.__defineSetter__(loop_columnName, setfunc);
  }
}

var defaultColumnPrefixes = {
  "flag": " | ",
  "refName": " ",
  "refSince": " ",
  "nextPayment": " ",
  "lastClick": " ",
  "totalClicks": " ",
  "average": " ",
  "clickText": " ",
  "average_1": " ",
  "average_2": " ",
  "RSA": " ",
  "SD": " ",
  "profit": "$"
};

var prefs = new function()
{
  // Referral listings. Column preferences
  this.columnPrefix = {};
  addSettersGetters(this.columnPrefix, 'columnPrefix',defaultColumnPrefixes);


  // Some logging to test whether the getters/setters work
  // note: getters appear to work, though setters appear to have the same problem 
  // as those encountered when attempting to define the getters 
  console.info('this.columnPrefix.flag = '+ this.columnPrefix.flag);
  console.info('this.columnPrefix.profit = '+ this.columnPrefix.profit);

  console.group();
  console.info('---');
  // set the flag prefix to something (usually) different each time
  this.columnPrefix.flag = new Date().getMilliseconds();
  console.info('---');
  console.groupEnd();

  console.info('this.columnPrefix.flag = '+ this.columnPrefix.flag);
  console.info('this.columnPrefix.profit = '+ this.columnPrefix.profit);
}

在Firebug中产生以下输出:

this.columnPrefix.flag = |
this.columnPrefix.profit = $

---

setValue_InStoredObject: arguments =    
["columnPrefix", "profit", 595]    
{"flag":" | ","refName":" ","refSince":" ","nextPayment":" ","lastClick":" ","totalClicks":" ","average":" ","clickText":" ","average_1":" ","average_2":" ","RSA":" ","SD":" ","profit":"$"}    
{"flag":" | ","refName":" ","refSince":" ","nextPayment":" ","lastClick":" ","totalClicks":" ","average":" ","clickText":" ","average_1":" ","average_2":" ","RSA":" ","SD":" ","profit":595}

---

this.columnPrefix.flag = |    
this.columnPrefix.profit = 595

我怀疑这是由与我的其他问题相同的问题引起的,但是我遇到了心灵障碍,似乎无法找到让它发挥作用的方法。

请帮忙吗? =]

1 个答案:

答案 0 :(得分:1)

尝试将var setFunc设置为for循环。

var setFunction = function(_prefName,loop_columnName){
    return function(_newValue){
        setValue_InStoredObject(_prefName,loop_columnName, _newValue)
    }
};

然后在循环中:

var setfunc = setFunction(_prefName,loop_columnName);
_obj.__defineSetter__(loop_columnName, setfunc);