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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:33:40+00:00 2026-06-14T19:33:40+00:00

Updated: This is an update to my previous question that was somewhat off topic

  • 0

Updated: This is an update to my previous question that was somewhat off topic as to what this StackExchange is aiming for. But I have a follow up question to the previous inquiry about this subject.

Object Model:

 var Soldier;

 Soldier = (function() {

    "use strict";

    function Soldier() {
        var privateVar = "privateValue";

        this.methodToGetPrivateValue = function() {
            return privateVar;
        }
    }

    var sharedPrivateVar = "sharedPrivateValue";

    function sharedPrivateMethod() {
        // I want to get value `privateVar`
    }

    Soldier.prototype = {
        publicVar: "publicValue",

        publicMethod: function() {
            return this.publicVar;
        },

        sharedPrivate: function() {
            return sharedPrivateVar;
        }
    }

    return Soldier;

 })();

 var marine = new Soldier();

So my updated question to make this topic more a proper question is if there is anyway to get a sharedPrivateMethod defined in this way to be able to access the private variable in the above setup?

The reason I am asking is that the sharedPrivateMethod is totaly invisible to the instanced object. While the function defined inside Soldier() is accessible to the instance because of the this.method = function(). I dont know if it has any real use at the moment but would be interesting to see if it was possible somehow.

  • 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-14T19:33:42+00:00Added an answer on June 14, 2026 at 7:33 pm

    The problem with what you have there is that your _self variable is shared by all instances constructed via new Test, and so for instance, assume your privateMethod used it:

    function privateMethod() {
        console.log(_self.message);
    }
    

    Then this:

    var t1 = new Test();
    t1.message = "Message 1";
    var t2 = new Test();
    t2.message = "Message 2";
    
    t1.privateMethod();
    

    …would log “Message 2”, not “Message 1” as you would expect, because the second call to new Test has overwritten the _self_ variable.

    Other than the _self variable, what you have is fine. It lets you have private data and functions shared by all instances, which is very handy. If you need to have truly private data that’s specific to each instance, you need to create the function that uses that data in the constructor function itself:

    function Test() {
        var trulyPrivate = 42;
    
        this.showTrulyPrivate = function() {
            console.log("trulyPrivate = " + trulyPrivate);
        };
    }
    

    Then trulyPrivate is genuinely private to the instance. The cost is the cost of creating a showTrulyPrivate function for each instance. (The function objects may be able to share the underlying code, a good engine will do that, but there will be separate function objects.)

    So to wrap up:

    var Test = (function() {
        // Data and functions defined here are private to this code
        // and shared across all instances. There is only one copy
        // of these variables and functions.
        var privateDataSharedByAll;
    
        function privateFunctionSharedByAll() {
        }
    
        function Test() {
            // Data and functions here are created for *each* instance
            // and are private to the instance.
            var trulyPrivate;
    
            this.hasAccessToTrulyPrivate = function() {
                // Has access to the shared private data *and* the
                // per-instance private data.
            };
        }
    
        return Test;
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is an updated version of a previous question I have asked on
This is a follow up to a previous question that I had before about
NOTE: This is the simple version of my previous question that SHOULD have been
this is an update to the previous question that I had about locating peaks
I have deleted my previous question and post this updated: I have a an
Update: I updated this after doing some digging and realizing that this might be
UPDATE: This question is out of date, but left for informational purposes. Original Question
Update: This question was an epic failure, but here's the working solution. It's based
This is related to my previous question . I'm trying, in Excel, to update
This is a follow up to my previous AngularJS question . I am trying

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.