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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T15:26:52+00:00 2026-05-10T15:26:52+00:00

To make a JavaScript class with a public method I’d do something like: function

  • 0

To make a JavaScript class with a public method I’d do something like:

function Restaurant() {}  Restaurant.prototype.buy_food = function(){    // something here }  Restaurant.prototype.use_restroom = function(){    // something here } 

That way users of my class can:

var restaurant = new Restaurant(); restaurant.buy_food(); restaurant.use_restroom(); 

How do I create a private method that can be called by the buy_food and use_restroom methods but not externally by users of the class?

In other words, I want my method implementation to be able to do:

Restaurant.prototype.use_restroom = function() {    this.private_stuff(); } 

But this shouldn’t work:

var r = new Restaurant(); r.private_stuff(); 

How do I define private_stuff as a private method so both of these hold true?

I’ve read Doug Crockford’s writeup a few times but it doesn’t seem like ‘private’ methods can be called by public methods and ‘privileged’ methods can be called externally.

  • 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. 2026-05-10T15:26:53+00:00Added an answer on May 10, 2026 at 3:26 pm

    You can do it, but the downside is that it can’t be part of the prototype:

    function Restaurant() {     var myPrivateVar;      var private_stuff = function() {  // Only visible inside Restaurant()         myPrivateVar = '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         private_stuff();     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The trick with these kinds of situations is to turn… May 11, 2026 at 10:21 am
  • added an answer The event bubbles up, until it gets handled... Since the… May 11, 2026 at 10:21 am
  • added an answer You could make your enums implement an interface, then give… May 11, 2026 at 10:21 am

Related Questions

To make a JavaScript class with a public method I'd do something like: function
I want to make a JavaScript application that's not open source, and thus I
I want to make a link call a Javascript function through the onclick event
Without using Javascript, is there a way to make a CSS property toggle on
I've run into a problem where I make changes to a few JavaScript files
I create a file in JavaScript. Now I want to be able to make
How can you make the ASP.net Session Data available to a JavaScript method? I
To make a long story short I have to use processing in a class
Trying to make a make generic select control that I can dynamically add elements
Trying to make a MySQL-based application support MS SQL, I ran into the following

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.