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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:56:27+00:00 2026-06-14T15:56:27+00:00

I have been trying to emulate static properties in javascript. It has been mentioned

  • 0

I have been trying to emulate static properties in javascript.
It has been mentioned in several places that class.prototype.property will be static across all objects inheriting from the class. But my POC says otherwise. Please take a look:

Using Class.prototype.property

//Employee class
function Employee() {
    this.getCount = function(){
        return this.count; 
    };      
    this.count += 1;
}
Employee.prototype.count = 3;

var emp = [], i;
for (i = 0; i < 3; i++) {
    emp[i] = new Employee();
    console.log("employee count is "+ emp[i].getCount());
}
/*Output is:
employee count is 4
employee count is 4
employee count is 4*/

My Question #1: If this were to be static, then shouldn’t the value of count have been 4,5,6,etc as all the objects share the same count variable?

Then I did another POC with Class.prototype and I think this to be static.

Using Class.property

//Employee class
function Employee() {
    this.getCount = function(){
        return Employee.count; 
    };      
    Employee.count++;
}
Employee.count = 3;

var emp = [], i;
for (i = 0; i < 3; i++) {
    emp[i] = new Employee();
    console.log("employee count is "+ emp[i].getCount());
}
/*Output is:
employee count is 4
employee count is 5
employee count is 6*/

My Question #2: Nowhere I have seen class.property being used directly. How exactly are static variables made in javascript keeping in mind my above code?

Or Have I coded something wrong here? Is this not the correct perception?

  • 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-14T15:56:29+00:00Added an answer on June 14, 2026 at 3:56 pm

    My Question #1: If this were to be static, then shouldn’t the value of count have been 4,5,6,etc as all the objects share the same count variable?

    Prototype properties are shared across instances, but if an instance has its own copy of the property, it will use that instead. Assigning to the property on the instance gives it its own copy, and so it doesn’t use the prototype’s anymore.

    The +=, ++, and similar operators result in assignments, and so they cause this behavior as well.

    Consider:

    function Employee() {
    }
    Employee.prototype.count = 0;
    

    As of the code above, there is an object in memory for Employee.prototype. Some ASCII art:

    +−−−−−−−−−−−−−−−−−−−−+
    | Employee.prototype |
    +−−−−−−−−−−−−−−−−−−−−+
    | count: 0           |
    +−−−−−−−−−−−−−−−−−−−−+

    Then we do this:

    var e = new Employee();
    

    Now there’s a second object in memory, which has a reference back to Employee.prototype:

    +−−−−−−−−−−−−−−−+
    |     e         |
    +−−−−−−−−−−−−−−−+          +−−−−−−−−−−−−−−−−−−−−+
    | [[Prototype]] |−−−−−−−−−>| Employee.prototype |
    +−−−−−−−−−−−−−−−+          +−−−−−−−−−−−−−−−−−−−−+
                               | count: 0           |
                               +−−−−−−−−−−−−−−−−−−−−+

    And if you query e.count:

    console.log(e.count);
    

    …since e doesn’t have its own property called count, the engine looks at e‘s prototype to find it, finds it, and uses that value.

    However, when we do this:

    e.count += 1; // Or more idiomatically, `++e.count;` or `e.count++;`
    

    That assigns a value to count on the e instance. e now has its own copy of count:

    +−−−−−−−−−−−−−−−+
    |     e         |
    +−−−−−−−−−−−−−−−+
    | count: 1      |          +−−−−−−−−−−−−−−−−−−−−+
    | [[Prototype]] |−−−−−−−−−>| Employee.prototype |
    +−−−−−−−−−−−−−−−+          +−−−−−−−−−−−−−−−−−−−−+
                               | count: 0           |
                               +−−−−−−−−−−−−−−−−−−−−+

    Now if you query e.count:

    console.log(e.count);
    

    …the engine finds count on e, and doesn’t look at the prototype.

    You can see this effect in code:

    function Employee() {
    }
    Employee.prototype.count = 0;
    
    var e = new Employee();
    console.log(e.hasOwnProperty('count')); // false
    e.count += 1;
    console.log(e.hasOwnProperty('count')); // true
    
    console.log(e.count);                   // 1
    console.log(Employee.prototype.count);  // 0

    This is also fun:

    var e = new Employee();
    
    console.log(e.count);                   // 0
    ++Employee.prototype.count;
    console.log(e.count);                   // 1

    Since e doesn’t (yet) have its own copy of count, if we actually increment the property on Employee.prototype, we see the updated value whether we ask for it directly (Employee.prototype.count) or indirectly (e.count).

    Final note on this: If e gets its own copy of a property, you can remove it again:

    var e = new Employee();
    console.log(e.count);    // 0, because it's using `Employee.prototype.count`
    ++e.count;               // Now `e` has its own `count` property
    console.log(e.count);    // 1, `e`'s own `count`
    delete e.count;          // Now `e` doesn't have a `count` property anymore
    console.log(e.count);    // 0, we're back to using `Employee.prototype.count`

    delete would be more properly called remove. It removes properties from objects.

    My Question #2: Nowhere I have seen class.property being used directly. How exactly are static variables made in javascript keeping in mind my above code?

    Two ways:

    1. Exactly as you’ve done it, Employee.sharedProperty.

    2. By defining the entire “class” inside a function, and using local variables within that function:

      var Employee = (function() {
          var sharedVariable = 0;
      
          function Employee() {
              ++sharedVariable;
              console.log("sharedVariable = " + sharedVariable);
          }
      
          return Employee;
      })();
      

      All functions defined within that outer scoping function will have access to the local variables defined within it. So there’s just one variable, the local within the one call to that outer function that creates Employee.

      Then, this code:

      new Employee();
      new Employee();
      new Employee();
      new Employee();
      

      outputs

      1
      2
      3
      4
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have been trying to encrypt an xml file to a string so that I
I have been trying to make custom radio buttons using HTML, CSS, and JavaScript.
I have been trying to serialize a list that contains arrays and lists. I
I have been trying to create a form that having 4 elements. One is
I have been trying to do a little project that needs an appendable ObjectOutputStream.
I have a situation that has been partially covered by other answers at SO,
Have been trying to fix this for the past hour. Here goes: <script type=text/javascript>
I have been trying to create a simple app that parse an entire channel
Have have been trying to make a validator for my xml files. I have
I have been trying to setup git for our web development team unsuccessfully. Some

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.