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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:50:34+00:00 2026-05-16T14:50:34+00:00

I think I have misunderstood how Javascript prototypal inheritance works. Specifically, the prototypes internal

  • 0

I think I have misunderstood how Javascript prototypal inheritance works. Specifically, the prototypes internal variables seem to be shared between multiple different sub-objects. It is easiest to illustrate with code:

var A = function()
{
  var internal = 0;
  this.increment = function()
  {
    return ++internal;
  };
};

var B = function() {};
// inherit from A
B.prototype = new A;

x = new B;
y = new B;

$('#hello').text(x.increment() + " - " + y.increment());​

This outputs 1 - 2 (test it on JSBin), while I fully expected the result to be 1 - 1, since I wanted two separate objects.

How can I make sure that the A object isn’t shared object between multiple instances of B?

Update: This article highlights some of the issues:

The problem is that the scope each approach uses to create a private variable, which works fine, is also the closure, in action, that results in if you change a private variable for one object instance, it is being changed for all. I.e. it’s more like a private static property, than an actual private variable.

So, if you want to have something private, more like a non-public constant, any of the above approaches is good, but not for actual private variables. Private variables only work really well with singleton objects in JavaScript.

Solution: As per BGerrissen’s answer, changing the declaration of B and leaving of the prototype works as intended:

var B = function() { A.apply(this, arguments); };
  • 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-16T14:50:35+00:00Added an answer on May 16, 2026 at 2:50 pm

    Private members are tricky using prototypical inheritance. For one, they cannot be inherited. You need to create private members in each individual constructor. You can do this by either applying the super constructor in the subclass or create a decorator.

    Decorator example:

    function internalDecorator(obj){
        var internal = 0;
        obj.increment = function(){
            return ++internal;
        }
    } 
    
    var A = function(){
        internalDecorator(this);
    }
    A.prototype = {public:function(){/*etc*/}}
    
    var B = function(){
        internalDecorator(this);
    }
    B.prototype = new A(); // inherits 'public' but ALSO redundant private member code.
    
    var a = new B(); // has it's own private members
    var b = new B(); // has it's own private members
    

    This is just a variation of the super constructor call, you can also achieve the same by calling the actual super constructor with .apply()

    var B = function(){
        A.apply(this, arguments);
    }
    

    Now by applying inheritance through B.prototype = new A() you invoke needless constructor code from A. A way to avoid this is to use Douglas Crockfords beget method:

    Object.beget = function(obj){
        var fn = function(){}
        fn.prototype = obj;
        return new fn(); // now only its prototype is cloned.
    }
    

    Which you use as follows:

    B.prototype = Object.beget(A.prototype);
    

    Of course, you can abandon inheritance altogether and make good use of decorators, at least where private members are needed.

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

Sidebar

Ask A Question

Stats

  • Questions 514k
  • Answers 514k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Repeater rpt; rpt.DataSource = new[] { new { ID =… May 16, 2026 at 6:06 pm
  • Editorial Team
    Editorial Team added an answer This part of your pointcut: within(tld.mycompany.business..*Impl) targets all joinpoints in… May 16, 2026 at 6:06 pm
  • Editorial Team
    Editorial Team added an answer This is the code you need (I corrected some of… May 16, 2026 at 6:06 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I think I may have misunderstood something here... But here goes. I'm using the
I think I have a pretty simple goal but cant seem to reach it.
I have a piece of JavaScript code which is expected to set an integer
I usually do not have difficulty to read JavaScript code but for this one
I must have misunderstood something. I simply want to use performSelector: to call one
I think I may have a misunderstanding of <xsl:variable\> and <xsl:value-of\> so perhaps someone
I think I have a problem with my makefile. I'm writing this program: Q2.cpp
think about having a skript where you have some words as input. now you
I have an image of 4x4 in size and I want to use it
I want to have a simple function where i can insert a sqlQuery and

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.