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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:14:11+00:00 2026-05-27T15:14:11+00:00

I am sure that this must be a pretty common question but after scouring

  • 0

I am sure that this must be a pretty common question but after scouring the internets for several hours, I have not found an answer. Here is the question:

Suppose that I have an interface called mammal. Every Mammal has to be able to sleep and eat. (In the future I may throw exceptions for the Mammal class to force children to implement the function).

function Mammal() {};

Mammal.prototype.eat = function() {};

Mammal.prototype.sleep = function() {};

Now suppose that I have a Dog class who implements the Mammal class:

function Dog() {};

Dog.prototype = new Mammal();

Dog.prototype.eat = function() {
    ...
};

Dog.prototype.sleep = function() {
    ... 
};

The dog’s eat function is very complicated and I would like to use a helper function. I have not been able to figure out what the best way to do this is. Here are the points of consideration:

  • The helper function should never be called from outside of the dog class so ideally it should be private.
  • The eat function does not have access to private functions (prototypes do not have access to private functions)
  • I could put the helper function into a privalaged function but:
    • This would still be a public function -> ie: everyone has the right to call it
    • It would not be part of the prototype so every dog would need to have its own helper function. If there were lots of dogs this seems inefficient.
  • I cannot make the eat function a privaliged function because in order for prototype inheritance to work it needs to be part of the prototype.

Question: How can I call a private function from a prototype function? Or more generally: When an object (child object) inherits from another object (parent object) how should children methods use helper functions and is it possible to make these private?

  • 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-27T15:14:12+00:00Added an answer on May 27, 2026 at 3:14 pm

    Define your Dog “class” in a closure. Then you can have shared priveliged functions. Just know you will have to be careful about this binding properly when you call it.

    var Dog = (function() {
      function Dog() {};
    
      // Common shared private function, available only to Dog.
      var privateHelper = function() { ... };
    
      Dog.prototype = new Mammal();
    
      Dog.prototype.eat = function() {
        privateHelper()
        // Or this if the helper needs to access the instance.
        // privateHelper.call(this);
        ...
      };
    
      return Dog;
    })();
    

    A function on a prototype is still just a function. So follows the same scope and closure access rules as any another function. So if you define your entire Dog constructor and prototype and a secluded island of scope that is the self executing function, you are welcome to share as much as you like without exposing or polluting the public scope.

    This is exactly how CoffeeScript classes compile down to JS.

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

Sidebar

Related Questions

I'm pretty sure that this question is very noob but I'm not used to
I am sure that this kind of questions must have been asked before, but
I see this question here on SO but I am not sure that answers
I'm pretty sure this must have been here already, but I didn't find much
Not too sure how to formulate my question and I hope that this is
I am not sure where i've seen this before, but im sure that there
After reading this article on thedailywtf.com, I'm not sure that I really got the
Not sure this is possible, but looking to write a script that would return
I am sure this must have been answered before but I cannot find a
I have this function I'm using and I want to be sure that it

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.