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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:37:19+00:00 2026-05-11T22:37:19+00:00

After reading a bit on Javascript’s prototypical inheritance model , I change my style

  • 0

After reading a bit on Javascript’s prototypical inheritance model, I change my style of constructing a class from

var Some_Class = function() {
    this.public_method = function() {
    };
    (function() {
        // constructor
    }).call(this)
}

to

var Some_Class = function() {
    (function() {
        // constructor
    }).call(this)
}
Some_Class.prototype.public_method = function() {
};

Although I understand that this is a good practice, but I am not allowed to access private methods from the public method anymore

var Some_Class = function() {
    var private_member = 'whatever';

    (function() {
        // constructor
    }).call(this)
}
Some_Class.prototype.public_method = function() {
    return private_member; // not possible
};

After reading through an article here (Closure-created constructor), then I came out with this

var Some_Class = function() {
    var private_member = 'whatever',

    private_method = function(_some_value) {
        // private method implementation
    };

    if(!arguments.callee.prototype.public_method) {
        arguments.callee.prototype.public_method = function() {
            private_method.call(this, private_method);
        };
    }

    (function() {
        // constructor
    }).call(this)
}

However, what are the drawbacks of doing this?! or is there a better way of doing this if I want to access private member in the public method?

  • 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-11T22:37:19+00:00Added an answer on May 11, 2026 at 10:37 pm

    The use of function scope variables and closures to simulate private variables/functions is a well established idiom in the javascript community. If the variable is truly intended to be private, I see no drawback to this approach (although some claim that performant code on certain browsers/hosts has to pay attention to how many closures get created).

    In your example, the private_method (and its environment) is shared across all objects – since your public_method closure is created only the first time the object is constructed (and bound to the constructor’s prototype property that sets the created object’s internal prototype chain) – so the private_method that is used is only the one that was created the first time.

    Here is some sample code that will help illustrate what is going on:

      var global = 1;
    
      var Some_Class = function() {
        var private_method = 'whatever';
        var now = ++global;
        print("outer now: " + now );
        private_method = function(_some_value) {
            // private method implementation
            print("inner now: " + now);
        };
    
        if(!arguments.callee.prototype.public_method) {
            arguments.callee.prototype.public_method = function() {
    
                private_method.call(this, private_method);
            };
        }
    
        (function() {
            // constructor
        }).call(this)
    }
    
    new Some_Class().public_method(); // outer now: 2, inner now: 2
    new Some_Class().public_method(); // outer now: 3, inner now: 2
    new Some_Class().public_method(); // outer now: 4, inner now: 2
    
    

    Are you sure that is what you want?

    If your private_method does not need to refer to the enclosing object’s state, then I see little benefit in doing things the way you are doing.

    What I usually do (if i have to use ‘new’ to create my object) is the following:

    function MyClass() {
      var private_var = 1; 
      function private_func()
      {
    
      }
      this.public_func = function() 
      {
         // do something
         private_func();
      }
      this.public_var = 10;
    }
    
    var myObj = new MyClass();
    
    

    The downside to this approach is that each time you construct the object via ‘new’ you re-create all the closures. But unless my profiler tells me that this design choice needs to be optimized, i prefer its simplicity and clarity.

    Also I don’t see the benefit in your code of doing the following either:

      (function() { }).call(this);  // call the constructor
    

    Why are you creating a separate scope in your constructor?

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

Sidebar

Ask A Question

Stats

  • Questions 376k
  • Answers 376k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The easiest way to segment an image into regions is… May 14, 2026 at 8:29 pm
  • Editorial Team
    Editorial Team added an answer is an IIS 7 specific tag, so you should not… May 14, 2026 at 8:29 pm
  • Editorial Team
    Editorial Team added an answer Yes. See this article. Here's an example from there: Console.BackgroundColor… May 14, 2026 at 8:28 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.