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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:16:27+00:00 2026-06-06T09:16:27+00:00

I am essentially trying to set a function’s prototype within the function itself. The

  • 0

I am essentially trying to set a function’s prototype within the function itself. The purpose is to hide many functions inside one public function, yet keep the functionality.

Here is the non-working code in a jsFiddle. The intended behaviour is displaying “Greetings!” and “Take me to your leader.”

The code can be ‘fixed’ by moving the Outer.prototype lines and the Extensions function out of the Outer function to the global scope. However, this means the Extensions function is public and free for direct use, which it shouldn’t be.

What would be a good way to achieve this ‘private’ behaviour?

  • 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-06-06T09:16:29+00:00Added an answer on June 6, 2026 at 9:16 am

    This does not work because you are overriding the prototype. When you call new Outer, then this will inherit from the current Outer.prototype. You are then overriding Object.prototype inside the function, but it does not affect the currently created instance, only future ones.
    You’d have to extend the existing prototype.

    But if you want hide certain functions, use a self-invoking function:

    var Outer = (function() {
    
        function Extensions() {
        }
        Extensions.protoype.Greet = function () {
            alert(this.Greetings);
        }
    
        function Inner() {
            Extensions.call(this);
            this.Greetings = "Take me to your leader.";
        }
        Inner.prototype = Object.create(Extensions.prototype);
        Inner.prototype.constructor = Inner;
    
        function Outer() {
            Extensions.call(this);
            this.Greetings = "Greetings!";
        }
        Outer.prototype = Object.create(Extensions.prototype);
        Outer.prototype.constructor = Outer;
    
        Outer.prototype.getInner = function() {
            return new Inner();
        };
    
        return Outer;
    
    }());
    

    Here, Extensions is defined inside the immediately executed function. Therefore it is not accessible from the outside. Only Outer can be accessed since you return it from that function.

    See this answer for a very good explanation of inheritance done right in JavaScript.

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

Sidebar

Related Questions

I'm essentially trying to set it up so that when I click a button
Essentially i'm trying to get the value 'col' that is set in the html,
I'm trying to use a php function within a template html form that is
I'm trying to open a PPTX from a specific folder using a Function within
I am trying to cut down jQuery to a very specific set of functions
I'm trying to use lambdas in some VB.Net code, essentially I'm trying to set
I am essentially trying to set the digits value of an EditText programmatically. So
I am trying to essentially setup an IIS7 intranet CDN for a series of
I have the following code: Tag.find_all_by_company_id(4).each.collect{|tag| tag.name }.join(,) (Essentially I'm trying to build a
I'm trying to make a batch file through Notepad to essentially automate a process

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.