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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:35:31+00:00 2026-06-13T03:35:31+00:00

Just when I thought I had JS figured out, I get hung up on

  • 0

Just when I thought I had JS figured out, I get hung up on this:

function Obj() {
    console.log('x: %s, o.x: %s', this.x++, this.o.x++);
}    
Obj.prototype.x = 1;
Obj.prototype.o = {x: 1};

Expected:

> new Obj
x: 1, o.x: 1
> new Obj
x: 1, o.x: 1
> new Obj
x: 1, o.x: 1

Actual:

> new Obj
x: 1, o.x: 1
> new Obj
x: 1, o.x: 2
> new Obj
x: 1, o.x: 3

So, it seems that if a prototypal property is a reference type, then it is shared across all instances, yet if it’s a non-reference type then it is reinitialized for each instance; to confirm this hypothesis, I tested a few other types, such as string (which behaves like number) and array (which behaves like object).

I have determined that I can avoid this pitfall if I reinitialize the object property in the ctor, like this:

function Obj() {
    this.o = {x: 1};
}

Which just seems really unorthodox (that I’d be required to manually reinitialize properties but only if they’re a reference object).

Can anyone shed some light on what’s going on?

  • 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-13T03:35:32+00:00Added an answer on June 13, 2026 at 3:35 am

    Think of it this way. You’re always going to be modifying the object on which you’re operating, irrespective of where the value comes from.

    When you do this for the first time:

    this.x++;
    

    It is getting the value of x from Obj.prototype.x because there was no x property directly on the this object, nevertheless the ++ is still operating on the this object, so the value is set on that object. That value is now directly on the instance for future modifications. (The prototype.x is shadowed until the direct property is deleted.)

    However, when you do this:

    this.o.x++
    

    The only operation on the this object is the lookup of o. Since again there is no o property on this, you’ll get the reference to the object stored at Obj.prototype.o. At this point, there’s no actual modification of the this object.

    After the reference is returned, you then look up the property x on the Obj.prototype.o object, and modify it.

    So it’s actually quite consistent. With this.o.x++ you’ve performed a lookup on this, but no mutation. The mutation is on the referenced object. But with this.x++, you’re mutating the object directly.


    So yes, unless you want your reference types to be shared among all instances created from the constructor, you should put the reference types directly on the instances and not on the .prototype.

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

Sidebar

Related Questions

I thought I had this figured out but it turns out I'm just deleting
Sigh... just when I thought I had figured out all the issues with trailing
I thought I had this figured out, but I'm wanting to find all occurances
I thought I had it figured out but now I'm missing something. First I
Just wondering if you had any thoughts on this problem. I want to make
I probably just haven't thought this through, or perhaps I'm simply unaware of an
Probably I'm just too dump for googling, but I always thought char arrays get
This problem is very simple, but I just can't figure it out added to
This was baffling me a lot . I just figure out what was happening,
I just thought about the non-blocking infrastructure of tornado and event-driven programming. Actually I'm

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.