在循环中定义Setter
作为 在循环中设置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将
var setFunc
设置为 for 循环之外。然后在循环中:
Try setting
var setFunc
out of the for loop.Then in the loop: