Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1056813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:46:13+00:00 2026-05-16T17:46:13+00:00

As a followup to Setting javascript defineGetter and defineSetter in a loop I’m now

  • 0

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? =]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:46:14+00:00Added an answer on May 16, 2026 at 5:46 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.