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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:46:44+00:00 2026-05-26T05:46:44+00:00

I have an object containing both public and private variables. The public variables are

  • 0

I have an object containing both public and private variables. The public variables are assigned to the private variables (I think), however, whenever I modify the private variables with a function, the public variables don’t update.

var foo = (function() {
    //Private vars
    var a = 1;

    return {
        //Public vars/methods
        a: a,
        changeVar: function () {
            a = 2;
        }
    }
})();
alert(foo.a);  //result: 1
foo.changeVar();
alert(foo.a);  //result: 1, I want it to be 2 though

Now I know that if I change the line in changeVar to this.a = 2; it works but then it doesn’t update the private variable. I want to update both the private and public variables at the same time. Is this possible?

JsFiddle showing problem

  • 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-26T05:46:45+00:00Added an answer on May 26, 2026 at 5:46 am

    When you set the a key in the object you’re returning, that’s making a copy of the ‘private’ a variable.

    You can either use a getter function:

    return {
        //Public vars/methods
        a: function() { return a; },
        changeVar: function () {
            a = 2;
        }
    };
    

    Or you can use Javascript’s built-in accessor functionality:

    obj = {
        //Public vars/methods
        changeVar: function () {
            a = 2;
        }
    };
    Object.defineProperty(obj, "a", { get: function() { return a; } });
    return obj;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ResultSet object containing all the rows returned from an sql query.
I have a populated SqlCommand object containing the command text and parameters of various
I have a simple JavaScript Array object containing a few numbers. [267, 306, 108]
I have a object built through a factory containing my parameters read from the
I have a GridView where one column is bound to an object property containing
I have a window containing a textBox. On both the window AND the textBox,
In Java I have two classes: Class A { public String ID; public Object
I have been struggling with the following problem: I have a value object containing
I have a JSON object of data containing items and their sales listed by
I current have an object that has a public property called Id. When I

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.