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

  • Home
  • SEARCH
  • 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 8742795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:33:45+00:00 2026-06-13T11:33:45+00:00

is this possible? We sure know how to emulate private members in javascript: var

  • 0

is this possible?

We sure know how to emulate private members in javascript:

var Class = (function() {
    var _private;

    Class = function() {
        _private = {};
    };

    Class.prototype.set = function(name, value) {
        _private[name] = value;
    };

    Class.prototype.get = function(name) {
        return _private[name];
    };

    return Class;

})();

The side effect of this pattern is that when I create two instances:

var instanceOne = new Class();
var instanceTwo = new Class();

then the private property is shared between:

instanceOne.set('test', 'me');
instanceTwo.get('test');

Is there some work around for this problem?

Regards

  • 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-06-13T11:33:46+00:00Added an answer on June 13, 2026 at 11:33 am

    The standard way to have “private” members in JavaScript is to use local variables within the constructor and have anything that needs to access them defined as closures over the context of the call to the constructor, like this:

    function Class() {
        var privateValue = 0;
    
        this.getPrivateValue = function() {
            return privateValue;
        };
    }
    Class.prototype.doSomething = function() {
        // This function doesn't have access to `privateValue`
        // except through `getPrivateValue`, even though
        // it's accessible as a member of the instance
    };
    

    This has been described by many, probably most famously Douglas Crockford.

    Note that this has the repercussion that each instance of Class gets its own copy of the getPrivateValue function, because each is a closure over a different call to the constructor (which is why it works). (This doesn’t mean all the code of the function is duplicated, however; at least some engines — Google’s V8 engine, used in Chrome and elsewhere for instance — allow the same code to be shared by multiple function objects which have different associated contexts.)

    (Side note: I haven’t used the name private because it’s a reserved word. In ES3, it was a “future reserved word;” in ES5, it’s only one in strict mode; details.)

    I’ve used a simple variable above rather than an object to avoid making things look more complex than they are. Here’s the way the would apply to your example:

    function Class() {
        var privateData = {};
    
        this.get = function(name) {
            return privateData[name];
        };
    
        this.set = function(name, value) {
            privateData[name] = value;
        };
    }
    

    Or if you still want to also have class-wide private data shared across instances:

    var Class = function() {
        var dataSharedAmongstInstances;
    
        function Class() {
            var privateDataForEachInstance = {};
    
            this.get = function(name) {
                return privateDataForEachInstance[name];
            };
    
            this.set = function(name, value) {
                privateDataForEachInstance[name] = value;
            };
        }
    
        return Class;
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is possible but I'm not really sure where to start. Has
I'm not sure this is even possible but I know if it is, the
I know this is probably possible using Streams, but I wasn't sure the correct
I am not sure if this is possible, so please let me know. I
I'm not sure if this possible. If not, let me know. I have a
I know something like this is possible, I'm pretty sure I'm just wording it
I'm not sure if this is possible in C++. I know you can pass
I'm not sure if this possible but probably is fairly simple. I've made a
Not sure if this is possible: I want to use datatables to manipulate my
Not sure if this is possible. I want to grab the latest X stories

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.