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 6531405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:52:28+00:00 2026-05-25T09:52:28+00:00

I have a global var that is updated later on in my program, and

  • 0

I have a global var that is updated later on in my program, and a previous variable set that that global var uses the original referenced value.

To illustrate,

var testVar = 1;

var hash = { 
    test : { 
        testGetVar : {opacity: testVar},
        testGetVarFn : function(){ return {opacity: testVar}; }
    }
}

testVar = 2;
console.log(hash.test.testGetVar.opacity); // returns 1
console.log(hash.test.testGetVarFn().opacity); //returns 2

Would someone clarify the proper way to do this?
Let’s say if I had 10 objects in hash that uses testVar, would I have to write fn to get the updated value?

EDIT:
I’ve changed some requirements and made my example to specific to my cause.

Here’s getter/setter methods, but doesn’t work.

var testVar = new Field("123");

function Field(val){
        this.value = val;
}
Field.prototype = {
        get value(){
             return this._value;
    },
        set value(val){
             this._value = val;
    }
};

var hash = { 
    test : { 
        testGetVar : {opacity: testVar.value} ,
        testGetVarFn : function(){ return testVar.value; }
    }
}

testVar.value = "abc";
console.log(hash.test.testGetVar.opacity); // returns 123
console.log(hash.test.testGetVarFn()); //returns abc

My assumption is that since the get method called when creating the hash, it stores the reference to that value at that time, thus will never return the updated value

  • 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-25T09:52:28+00:00Added an answer on May 25, 2026 at 9:52 am

    You could turn testVar into an object, because objects are passed by reference in js, this way you would not need to call a function to get the latest value:

    var testVar = {'value':123};
    
    var hash = { 
        test : { 
            testGetVar : testVar,
            testGetVarFn : function(){ return testVar; }
        }
    }
    
    testVar.value='abc';
    console.log(hash.test.testGetVar.value); // returns 123 (not anymore: returns "abc" now)
    console.log(hash.test.testGetVarFn().value); //returns abc
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble with global variables in php. I have a $screen var set
If i have global variable in A.dll, that depends on global variable in B.dll
I have a global event created and set/reset in a native C++ process that
I have a global variable that is an instance of my custom class. How
I have a program that uses System.DirectoryServices.AccountManagement.PrincipalContext to verify that the information a user
In Actionscript you can have global variables like this: var number : Number =
I have some global variables in a Python script. Some functions in that script
I have a global header and footer that has a Database connection and a
I have a global pointer variable char* pointer = new char[500]; /* some operations...
I have a function that makes ajax GET request and based on the value

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.