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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:31:23+00:00 2026-06-04T18:31:23+00:00

Is there a correct way to create private static javascript variables (and functions) that

  • 0

Is there a correct way to create private static javascript variables (and functions) that do not change no matter how many times you create new Obj?

This is what I tried and it seems to work:

var ObjClass = (function(){

    var static_var = 0; //private static variable
    var static_fn = function(){ return static_var; }; //private static function

    return function(){
        ++static_var;
        var thisNumber = static_var;
        this.getThisNumber = function(){
             return thisNumber;
        }
        this.getStaticNumber = static_fn; //making static fn public
    }

})();

var obj1 = new ObjClass;
var obj2 = new ObjClass;
var obj3 = new ObjClass;

console.log(obj1.getThisNumber()); //output `1`
console.log(obj1.getStaticNumber()); //output `3`
console.log(obj2.getThisNumber()); //output `2`
console.log(obj2.getStaticNumber()); //output `3`
console.log(obj3.getThisNumber()); //output `3`
console.log(obj3.getStaticNumber()); //output `3`​

DEMO

Or is there some other better way?

  • 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-04T18:31:25+00:00Added an answer on June 4, 2026 at 6:31 pm

    Yes, that is the correct approach to create private static variables.

    However, I would treat the static_fn different. It seems you want it to be public.

    1. It should be on your “class”es prototype as it does not interact with private instance variables
    2. It even does not interact with instances at all. The usual approach is to put such a function/variable on the “class” itself, i.e. the constructor in JS. As the constructor is a Function object, it can be extended with properties as any other js object.
    var ObjClass = (function closure(){
    
        var static_var = 0; //static private (scoped) variable
        function static_fn(){ return static_var; }; //static private (scoped) function
    
        function ObjClass() {
            var thisNumber = ++static_var; // private instance variable
            this.getThisNumber = function() { // public instance method
                return thisNumber; // "privileged" to access scoped instance variables
            };
        }
        ObjClass.getStaticNumber = static_fn; // make the static_fn public
        return ObjClass;
    })();
    
    
    
    var obj1 = new ObjClass;
    var obj2 = new ObjClass;
    console.log(ObjClass.getStaticNumber()); //output `2`
    var obj3 = new ObjClass;
    console.log(ObjClass.getStaticNumber()); //output `3`
    
    console.log(obj1.getThisNumber()); //output `1`
    console.log(obj2.getThisNumber()); //output `2`
    console.log(obj3.getThisNumber()); //output `3`
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Firstly I know that there are many question and solutions to correct thread marshalling
Is there a correct way to getting the file size when uploading an image
I am looking for the correct way/structure to create a loop in a Thread/Task
I've a complex query and I can't get the correct answer. There are 3
What do you think? Is this correct or are there memory leaks? Source: #include
I have a class, class MyClass { private int val; public static final MyClass
What would be the best way to create unit tests for testing the parameters
I am trying to create a JTable that has a row header that looks
I'm trying to create a dynamic QSlider that accepts a QVector of integer values
I'm trying to figure out the correct way of applying locking to the following

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.