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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:54:39+00:00 2026-05-19T10:54:39+00:00

I’m building a system, with similar details as this question: Can I have different

  • 0

I’m building a system, with similar details as this question: Can I have different copies of a static variable for each different type of inheriting class

Except it’s in JavaScript!

I’m going to have a set of subclasses. Each subclass will have a lot of instances created of it during the life of the program. Each instance of a particular subclass will have different values, except for a file that is common to all. I don’t want each instance to have a copy. I want it static (one copy for all). Each different subclass uses a different file though. I want each subclass to inherit the FACT that it HAS a file from a superclass.

EDIT: Quick Summary:

goal: create a class that is a constructor that
constructs the definition of a
subclass, allowing a variable:
– Appearing in every subclass
– Unique to a particular subclass
– Shared by all instances of a particular subclass (one copy for all)

Attempt at illustrating this in code:

var instance1ofSubclass1 = {
    staticVar:"SAMEVAL_1", // For brevity I'm showing how staticVar is  
                           // the same. It's notsupposed to be public.
    uniqueVar:"adsfasdf"
};
var instance2ofSubclass1 = {
    staticVar:"SAMEVAL_1",
    uniqueVar:"zxbvczbxc"
};
var instance3ofSubclass1 = {
    staticVar:"SAMEVAL_1",
    uniqueVar:"qwrtytry"
};

var instance1ofSubclass2 = {
    staticVar:"SAMEVAL_2", //<--notice the static var is different
                           //   between subclasses
    uniqueVar:"oipoiuu"
};
var instance2ofSubclass2 = {
    staticVar:"SAMEVAL_2",
    uniqueVar:"hljkhlj"
};
var instance3ofSubclass2 = {
    staticVar:"SAMEVAL_2",
    uniqueVar:"bnmbmbmnm"
};

My class definitions could go like this:

var subclass1 = (function () {
    var staticVar = "SAMEVAL_1"
    return function (unique) {
        return {
            uniqueVar:unique
        };
    };
}());

var subclass2 = (function () {
    var staticVar = "SAMEVAL_2" //<-- different static variable
    return function (unique) {
        return {
            uniqueVar:unique
        };
    };
}());

Now, I want to go a step further and make a superclass for these classes. That’s where I’m stuck. I don’t want to have a staticVar definition in every subclass. I want to get it from the superclass.

  • 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-19T10:54:40+00:00Added an answer on May 19, 2026 at 10:54 am

    I think I’ve figured it out.

    Fiddle: http://jsfiddle.net/zXMaM/

    The pattern I’ve created sends a class definition through another function (essentially the super class) that creates an additional closure for the necessary private static variables and at the same time augments the object created by the class passed in to access the private static variables.

    It’s hard to explain as this is new to me too, but hopefully between the code and my comments it is followable.

    The “superclass” method:

    var superClass = function (yourPrivateStaticVar, classDef) {        
        var privateStatic = yourPrivateStaticVar;
    
        return function () { // return a new constructor (only invoked when a new 
                             // instance is created!)
    
            // run the constructor, grab the object it creates
            var newObj = classDef();
    
            // now, augment the object created (these vars/methods have no 
            // knowledge of the subclass!)
    
            //add private vars/methods to the object
            function getPrivStatic() {
                return privateStatic;
            }
    
           var example_inherited_private_var;
    
            //add public vars/methods to the object
            newObj.getPrSt_directly = function () {
                return privateStatic;
            }
    
            newObj.getPrSt_throughPrivateFunc = function () {
                return getPrivStatic();
            }
    
           newObj.example_inherited_public_var = "something";
    
            //return the augmented object (a new *instance* of the subclass)
            return newObj;
        };
    };
    

    Defining a “subclass”:

    //pass the definition of subclass1 through the superclass method and get it back
    var subClass1 = superClass("yourChosenPrivateStaticValue", function () {
        //private variables/methods go here
    
        return { //this is the actual instance object
            //public variables/methods go here
        };
    });
    

    Again, the fiddle: http://jsfiddle.net/zXMaM/

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I don't have much knowledge about the IPv6 protocol, so sorry if the question
Does anyone know how can I replace this 2 symbol below from the string
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) 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.