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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:29:02+00:00 2026-05-29T21:29:02+00:00

How do I inherit/extend classes that are using the Revealing Prototype pattern? And is

  • 0

How do I inherit/extend classes that are using the Revealing Prototype pattern?
And is there a way to make the private variables and functions protected?

Example base object:

myNameSpace.Person = function() {

    this.name= "";
    this.id = 0;

};

myNameSpace.Person.prototype = function(){
    var foo = function(){
        //sample private function
    };
    var loadFromJSON = function (p_jsonObject) {
       ...
    };
    var toJSON = function () {
       ...
    };
    var clone = function (p_other) {
       ...
    };

    return {
        loadFromJSON : loadFromJSON,
        toJSON: toJSON,
        clone: clone
    };
}();
  • 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-29T21:29:08+00:00Added an answer on May 29, 2026 at 9:29 pm

    There are no protected variables/properties in JavaScript. Though, you can reuse “private” variables when you declare the inheriting classes in the same scope, which seems possible in your case when the private variables are only “hidden utilities” of your prototype.

    MyNamespace.Person = function Person(params) {
        // private variables and functions, individual for each Person instance
        var anything, id;
        function execute_something() {}
    
        // public properties:
        this.name = "";
        this.getId = function getId(){
            // called a "privileged function", because it has access to private variables
        }
    }
    MyNamespace.American = function(params) {
        MyNamespace.Person.call(this, params); // inherit name and getId()
    }
    
    (function() { // new scope for
        // hidden utility functions and other private things
        function foo() { }
        function helpJSON() { }
        function fromJSON() { }
        var bar;
    
        (function(personProto) { // new scope for prototype module (not explicitly needed)
            // "private" /static/ variables (and functions, if you want them private)
            var personCount = 0;
    
            personProto.clone = function clone() {
                return this.constructor(myself); // or something
            };
            personProto.toJSON = function toJSON() {
                // use of helpJSON()
            };
            personProto.fromJSON = fromJSON; // direct use
        })(MyNamespace.Person.prototype);
    
        (function(amiProto) {
            // just the same as above, if needed
            amiProto.special = function() {
                // use foo() and co
            };
        })( MyNamespace.American.prototype = Object.create(MyNamespace.Person.prototype) );
    })();
    

    This is the JavaScript way of inheritance, which means American’s prototype inherits the clone(), toJSON() and fromJSON() functions automagically from the Person’s prototype. Of course overwritable. And the feature is

    new MyNamespace.American() instanceof MyNamespace.Person; // true
    

    Of course, if you don’t need that, and want use the more module-like way, you could reuse the utility functions, i.e. just copy them:

    (function() {
        // hidden utility functions and other private things
        var bar;
        var personCount;
        function foo() { }
        function helpJSON() { }
        function fromJSON() { }
        function clone() {
            return this.constructor(myself); // or something
        }
        function toJSON() { }
    
        (function(personProto) { // new scope, not really needed
            // private variables are useless in here
            personProto.clone = clone;
            personProto.toJSON = toJSON;
            personProto.fromJSON = fromJSON;
        })(MyNamespace.Person.prototype);
    
        (function(amiProto) { // new scope, not really needed
            // copied from personProto
            amiProto.clone = clone;
            amiProto.toJSON = toJSON;
            amiProto.fromJSON = fromJSON;
            // and now the differences
            amiProto.special = function() {
                // use foo() and co
            };
        })(MyNamespace.American.prototype);
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to have multiple classes that inherit/extends same class in Google AppEngine
I'm trying to inherit QSqlTableModel to make data im my table display in way
i have my auto-generated linq to sql classes, and i extend this class using
I have an abstract base class that many classes extend. I'd like all these
I'm trying to design a couple of classes that inherit a partial function, but
Is there a clean way to somehow use underscore.js _.extend function (or any other)
For my project I've trying to extend GWT classes that not inherited with client
When working with classes that inherit db.Model, is it better practice to add methods,
In my application, there are 10-20 classes that are instantiated once[*]. Here's an example:
I want to write some Javascript classes which extend DOM nodes (so that I

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.