在循环中定义Setter

发布于 2024-09-15 12:00:11 字数 3159 浏览 7 评论 0原文

作为 在循环中设置 javascript DefineGetter 和 DefineSetter 的后续操作,我现在在循环中定义 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

我怀疑这是由与我的其他问题相同的问题引起的,但我遇到了思维障碍,并且似乎无法找到使其工作的方法。

请帮忙? =]

As a followup to Setting javascript defineGetter and defineSetter in a loop I'm now having troubles with defining setters in a loop.

This is the code that I'm attempting to use within a Greasemonkey script.

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);
}

which produces the following output in 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

I suspect that this is caused by the same problem as my other question but I'm coming up against mind-blocks and cannot seem find a way to make it work.

Help please? =]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

带刺的爱情 2024-09-22 12:00:11

尝试将 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);

Try setting var setFunc out of the for loop.

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

Then in the loop:

var setfunc = setFunction(_prefName,loop_columnName);
_obj.__defineSetter__(loop_columnName, setfunc);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文