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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:13:01+00:00 2026-06-01T04:13:01+00:00

I have code that looks like this: var baseClass = function() { // CODE

  • 0

I have code that looks like this:

var baseClass = function() {
// CODE
    var subClass = function() {
        // MORE CODE
    }
}

Adding methods to baseClass is fine, I just use

baseClass.prototype.newMethod = function () {
    // NEW CODE
}

My question is how should I add methods to subClass? Is the only way to simply make it a public method?

######## EDIT ##############

OK so I’ve rearranged the code so the subClass is outside the baseClass. I pass in baseClass so subClass can still access the properties of the instance of baseClass.

var baseClass = function() {
    var base = this;
    this.property_a = 1;
    this.property_b = 5;

    var sub = new subClass(base);
    // CODE
}

var subClass = function(parent) {
    var sub = this;
    this.property_c = 1;
    this.method_a = function() {
        return sub.property_c + parent.property_a;
    }
    // MORE CODE
}

this is fine and works, but now I have a new problem of when I add a method using prototype:

subClass.prototype.method_b = function(){
    return sub.property_c + parent.property_b;
}

I get an error saying parent isn’t defined.

Basically I have a fairly simple web application that has two sides, a viewing side and an editing side. I build the base class which includes everything necessary for viewing, and I want to add the methods required for editing in a different file so they’re only loaded when a user is on the editing page.

  • 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-01T04:13:02+00:00Added an answer on June 1, 2026 at 4:13 am

    Why do you declare that subclass in the base class? Doesn’t make sense to me.

    You can add to the subclass’s prototype whereever it is in you scope. In your code it would be

    var baseClass = function() {
    // CODE
        var subClass = function() {
            // MORE CODE
        }
        subClass.prototype = {
            ...
        }
    }
    

    But I’d suggest to put it out of the base class constructor. If you want it private for some reason, add a closure:

    (function(){
    
        baseClass = function() { // public
            // CODE
        }
        baseClass.prototype = {...};
    
        var subClass = function() { // private
            // MORE CODE
        }
        subClass.prototype = Object.create(baseClass.prototype);
        subClass.prototype.newMethod = function () { ... }
    })()
    

    EDIT to answer the extended question:

    Ah, subClass doesn’t inherit from baseClass! We had expected that, otherwise it may be OK to have it inside the constructor. Then, the same prototype could have been added to each of the different subClass constructors:

    var subproto = {
        method_b: = function(){
            // manipulate "this"
        },
        ...
    };
    function baseClass() {
        // some code
        function sub() {
            // is a constructor for subs which belong to this specif base intance
            ...
        }
        sub.prototype = subproto; // the sub constructors of each base instance
                                  // have the same prototype
        var x = new sub(),
            y = new sub(); // example usage of the sub constructor
    }
    baseClass.prototype = {...}
    

    Else, if you want one common sub constructor (outside of function baseClass), you may give the base instance the sub belongs to as an argument to the constructor – as you did. Of course the sub (both internal and external methods) can only access public properties of that base instance.

    The mistake you made in your rearranged code is that your prototype (“external”) methods tried to access the private parent variable from the sub constructor. As you say, “error saying parent isn’t defined”.

    var subClass = function(parent) {
        var sub = this;
        this.parent = parent; // make it public
        this.property_c = 1;
        this.method_a = function() {
            return sub.property_c + parent.property_a;
        }
        // MORE CODE
    }
    subClass.prototype.method_b = function(){
        // prototype functions can only access public properties
        // i.e. privileged methods, public attributes and other prototype properties
        return this.property_c + this.parent.property_b;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that looks like this that works just fine: var info
i have javascript code that looks like this: $('#lnkPopup').click(function() { var id = $(this).attr('rel');
I have code that looks like this: var ds = new DataSet(); var fooIDToFoo
I have Delphi 2007 code that looks like this: procedure WriteString(Stream: TFileStream; var SourceBuffer:
I have a global variable MyGlobalVar and some code that looks like this: var
I have some C# code that looks something like this System.Data.DataRow row; var table
I have some code that looks like this: int i = 0; foreach (var
I have some very simple javascript code that looks like this: var newWindow =
I have code that looks somewhat like this (less ridiculous, of course): var d
I have some code (which works fine) that looks something like this: int integer

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.