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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:00:45+00:00 2026-05-26T10:00:45+00:00

Based on my understanding of javascript, prototype methods cannot access variables that are private

  • 0

Based on my understanding of javascript, prototype methods cannot access variables that are private to the scope of the constructor,

 var Foo = function() {
      var myprivate = 'I am private';    
      this.mypublic = 'I am public';
 }

 Foo.prototype = {
     alertPublic: function() { alert(this.mypublic); } // will work
     alertPrivate: function() { alert(myprivate); } // won't work
 }

It makes perfect sense, but is there any way around this that is safe and good practice? Since using prototypes provides a performance benefit in that the member functions are allocated only once, I’d like to achieve a similar functionality while still being able to get to my private variables. I don’t think it will work by using a prototype, but is there another pattern, such as a factory method or a closure approach? Something like,

var fooFactory = function() {
    var _alertPrivate = function(p) { alert(p); } // bulk of the logic goes here
    return function(args) {
         var foo = {}; 
         var myprivate = args.someVar; 
         foo.mypublic = args.someOtherVar; 
         foo.alertPrivate = function() { _alertPrivate(myprivate); };
         return foo; 
    }; 
}

var makeFoo = new fooFactory();
var foo = makeFoo(args); 

I’m not sure whether a new copy of _alertPrivate is created each time I create a new Foo or if there is any potential performance benefit. The intention is to get a functionality similar to prototyping (inasmuch as it saves memory) while still being able to access private variables.

Thanks.

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

    I have come up with the following pattern to address this issue, atleast for now. What I needed was a privileged setter so that a private variable could be changed from inside certain prototype functions but not from anywhere else:

     var Foo = (function() {
    
        // the bulk of the objects behavior goes here and is created once 
        var functions = {
            update: function(a) {
                 a['privateVar'] = "Private variable set from the prototype";
            }
        }; 
    
        // the objects prototype, also created once
        var proto = {
            Update: function() {
                 this.caller('update'); 
            }
        };
    
        // special function to get private vars into scope
        var hoist = function(accessor) {
            return function(key) {
                 return functions[key](accessor()); 
            }
        }
    
        // the constructor itself
        var foo = function foo() {
            var state = {
                privateVar: "Private variable set in constructor",
                // put more private vars here
            }
            this.caller = hoist(function(){
                return state;
            }); 
        }
    
        // assign the prototype
        foo.prototype = proto;
    
        // return the constructor
        return foo; 
    
     })(); 
    

    Basically a pointer to the objects internal state is hoisted to its prototype via a closure over a simple accessor function() { return state; }. Using the ‘caller’ function on any given instance allows you to call functions which are created only once but can still refer to the private state held in that instance. Its also important to note that no functions outside of the prototype could ever access the privileged accessor, since the ‘caller’ only accepts a key that refers back to the predefined functions which are in scope.

    Here are some benchmarks of this method to see how it compares to pure prototyping. These figures represent creating 80,000 instances of the object in a loop (note the object used for benchmarking is more complex than the one above, which was just for simplification purposes):

    CHROME:

    Closure Only – 2172ms

    Prototyping (above way) – 822ms

    Prototyping (std way) – 751ms

    FIREFOX:

    Closure Only – 1528ms

    Prototyping (above way) – 971ms

    Prototyping (std way) – 752ms

    As you can see the method is almost as fast as normal prototyping, and definitely faster than just using a normal closure that copies functions along with the instance.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on my understanding of the Java language, static variables can be initialized in
My understanding is that Java's implementation of regular expressions is based on Perl's. However,
My understanding is that one can use either of the two methods for case-insensitive
Based on my understanding, the basic difference between traps and interrupts is that the
Normally ( Based on my understanding ) i have to follow a lot of
From my understanding the XMPP protocol is based on an always-on connection where you
This question probably is based on my lack of understanding of the role of
I've been working with some ExternalDataExchange - based communication in WF recently. My understanding
Based on my understanding, both of them essentially do the same thing (lets us
My understanding is that Google Analytics (the tool I'd normally use for website analytics)

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.