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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:06:37+00:00 2026-06-17T17:06:37+00:00

I understand how private static mechanism works in javascript; but that screws in inheritance.

  • 0

I understand how private static mechanism works in javascript; but that screws in
inheritance.
For example:

var Car = (function() {
    var priv_static_count = 0;
    return function() {
        priv_static_count = priv_static_count + 1;   
    }
})();
var SedanCar = function () {};
SedanCar.prototype = new Car();
SedanCar.prototype.constructor = SedanCar;

Is there any way to avoid this pitfall ?

  • 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-17T17:06:39+00:00Added an answer on June 17, 2026 at 5:06 pm

    First of all, there’s no such thing as “private static” in JavaScript. What you use here is a simple closure, which is created by a Immediately-Invoked Function Expression.

    Your question is not quite clear, but I guess you want to count the Car instances that were created, and it doesn’t work, because when you instantiate the subclass the counter won’t increment (problem 1). Instead, the counter is incremented only once, when you define your subclass (problem 2).

    Since JavaScript has a prototype based inheritance model, you have to create an object that can be used as prototype. But I suggest you to do it without calling the parent class’ constructor (this will solve the second part of the problem). This is a very common pattern used everywhere in the JavaScript world (see Simple JavaScript Inheritance, Backbone, CoffeScript, etc.), and it is very simple to implement if you don’t need to support very old browsers (IE6-8). It goes like this:

    SedanCar.prototype = Object.create(Car.prototype)
    

    Now the first part of the problem is quite simple to fix. Just call the parent constructor every time the child is instantiated. This is also a quite good pattern, and it is built into many other languages (Java, etc.). In JavaScript, you will have to do it manually, like this:

    var SedanCar = function () {
        // Parent class initialization
        Car.call(this /*, and, other, arguments, to, the, parent, constructor */)
    
        // Child class initialization
    };
    

    This will call the parent constructor with this bound to the newly created object. The parent constructor does the initialization, and then the child does it’s share of the work. In your example, the parent will increment the counter as you would expect.

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

Sidebar

Related Questions

I don't understand the difference between A and B. A private static final CookieStore
I'm trying to access my class's private enum. But I don't understand the difference
The following function is used to compute memory usage. private static long getMemoryUse(){ putOutTheGarbage();
class SingleTon { private static $instance; private function __construct() { } public function getInstance()
I have a class that has a private static variable, private static counter =
If I have a class that contains private static data allocated on the heap
I have set up a bootstrapper as follows: private static IUnityContainer BuildUnityContainer() { var
I'm trying to understand why this code is not working. private static object _lock;
var Gadget = function () { // static variable/property var counter = 0; //
I understand you can't mock static methods using mockito. But the method I am

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.