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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:16:38+00:00 2026-05-24T16:16:38+00:00

I was experimenting with some examples and came across a problem that if we

  • 0

I was experimenting with some examples and came across a problem that if we want to add a function to a prototype it will not be able to access the private members of the constructor. I came across this solution. This seems to be a nice hack.

I tried out some other ways and I got the following:

var Restaurant = function()
{
    var myPrivateVar;
    var private_stuff = function()   // Only visible inside Restaurant()
    {
        return "I can set this here!";
    }
    Restaurant.prototype.use_restroom = function()   // use_restroom is visible to all
    {
        private_stuff();
    }
    Restaurant.prototype.buy_food = function()    // buy_food is visible to all
    {
        return private_stuff();
    }
}
var restaurant = new Restaurant();
restaurant.buy_food(); // this would work
restaurant.private_stuff(); // this won't

The solution seems weird because we are adding to the prototype within the constructor function. (I haven’t seen much of this). It works on firefox 5 and chrome at least. Is there something wrong with it?

  • 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-24T16:16:39+00:00Added an answer on May 24, 2026 at 4:16 pm

    What you’re doing is redefining those methods on the prototype every time you make a new restaurant object. The more sane way to do that would be to define them on this, which is the new object being constructed in a constructor:

    var Restaurant = function()
    {
        var myPrivateVar;
        var private_stuff = function()   // Only visible inside Restaurant()
        {
            return "I can set this here!";
        }
        this.use_restroom = function()   // use_restroom is visible to all
        {
            private_stuff();
        }
        this.buy_food = function()    // buy_food is visible to all
        {
            return private_stuff();
        }
    }
    

    You could just do it like this though, and not use new:

    var RestaurantMaker = function () {
      var myPrivateVar;
      var private_stuff = function() {
        return "I can set this here!";
      }
    
      return {
        use_restroom: function () {
          private_stuff();
        },
        buy_food: function () {
          return private_stuff();
        }
      };
    }
    

    and then just do:

    var restaurant = RestaurantMaker();
    

    This is called the revealing module pattern. The downside is that each new object gets a copy of all the functions, which also happens if you add methods to this in your constructor.

    A very small alternative version of the revealing module pattern (which I think reads a bit better) looks like this:

    var RestaurantMaker = function () {
      var myPrivateVar;
    
      function private_stuff() {
        return "I can set this here!";
      }
    
      function use_restroom() {
        private_stuff();
      }
    
      function buy_food() {
        return private_stuff();
      }
    
      return {
        use_restroom: use_restroom,
        buy_food: buy_food
      };
    }
    

    Then, if you want to change whether a function is private or not, it’s just a matter of adding or removing it from the returned object.

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

Sidebar

Related Questions

I'm experimenting with some multithreading constructions, but somehow it seems that multithreading is not
I am doing some experimenting (so the code below has lots of stuff that
I am experimenting with changing some stylesheets from javascript I have come across a
I'm experimenting with some text to speech applications on android, where my app will
After going through some examples on the web I realize that there is a
After experimenting with some triangulation work I ran across the question on how to
Our team has been experiencing a recurring problem with velocity templates. Upon rendering, some
While experimenting with this question on collections in Spring.NET , I discovered that Spring
I'm experimenting with OpenSSL on my network application and I want to test if
I've been doing some experimenting with php and html, and I'd like to know

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.