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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:57:13+00:00 2026-05-10T18:57:13+00:00

I’m having a bit of trouble trying to get class variables to work in

  • 0

I’m having a bit of trouble trying to get class variables to work in javascript.

I thought that I understood the prototype inheritance model, but obviously not. I assumed that since prototypes will be shared between objects then so will their variables.

This is why this bit of code confuses me.

What is the correct way to implement class variables?

function classA() {};  classA.prototype.shared = 0;  a = new classA;  //print both values to make sure that they are the same classA.prototype.shared; a.shared;  //increment class variable classA.prototype.shared++;  //Verify that they are each 1 (Works) classA.prototype.shared; a.shared;  //now increment the other reference a.shared++;  //Verify that they are each 2 (Doesn't Work) classA.prototype.shared; a.shared; 

UPDATE: So it seems that everyone is confirming the fact that by incrementing the instance’s variable we don’t affect the prototype. This is fine, this is what I have documented in my example, but doesn’t this seem like an error in the design of the language? Why would this behavior be desirable? I find it weird that when the instance’s var is undefined we follow the hidden link to the prototype where we get the value of the var, but we copy it into the instance object.

I also understand that this isn’t java/c++/ruby/python, it’s a different language. I’m just curious as to why this behavior might be good.

  • 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. 2026-05-10T18:57:13+00:00Added an answer on May 10, 2026 at 6:57 pm
    I assumed that since prototypes will be shared between objects then so will their variables. 

    They are, but this:

    a.shared++ 

    is not doing what you think it’s doing. It’s in fact (approximately) sugar syntax for:

    (a.shared= a.shared+1)-1 

    (the -1 being to return the pre-increment value, not that you’re actually using the retrun value, but still.)

    So this is actually doing an assigment to a.shared. When you assign to an instance member you are always writing to that instance’s own members, not touching any members of any of its prototypes. It’s the same as saying:

    classA.prototype.shared= 1; a.shared= 2; 

    So your new a.shared hides the prototype.shared without altering it. Other instances of classA would continue to show the prototype’s value 1. If you deleted a.shared you would once again be able to see the prototype’s variable that was hidden behind it.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to create an if statement in PHP that prevents a single post
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has

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.