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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:09:00+00:00 2026-06-01T02:09:00+00:00

I have the following code (ObjA) and it works as I would expect, instance

  • 0

I have the following code (ObjA) and it works as I would expect, instance ‘a1’ has the same properties as ‘a2’ but with different values.

function ObjA() {
    this.objLiteral = {};
    this.propA = 0;
}

var a1 = new ObjA();
a1.objLiteral['hello'] = 3;
a1.propA = 1.5;

var a2 = new ObjA();
a2.objLiteral['goodbye'] = 4;
a2.propA = 2;

debugger info for a1 and a2:
http://www.flickr.com/photos/76617756@N02/6879283032/

Next, I have the following ObjB that inherits from ObjA. Instance ‘b1’ and ‘b2’ have the same properties and different values for properties propA and propB but for some reason, objLiteral is the same in both as if it was referencing the same object.

ObjB.prototype = new ObjA();
ObjB.prototype.constructor=ObjB;

function ObjB() {
    this.propB = 2;
}

var b1 = new ObjB();
b1.objLiteral['hello2'] = 6;
b1.propA = 4;
b1.propB = 5;

var b2 = new ObjB();
b2.objLiteral['goodbye2'] = 8;
b2.propA = 6;
b2.propB = 7;

debugger info for b1 and b2:
http://www.flickr.com/photos/76617756@N02/6879283088/

Can somebody help me understand what is happening? What do I have to do to ge what I am expecting?
Your help is much appreciated.

  • 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-01T02:09:02+00:00Added an answer on June 1, 2026 at 2:09 am

    Well, both objects b1 and b2 have the same prototype, namely an instance of ObjA:

    ObjB.prototype = new ObjA();
    

    hence they inherit and have access its properties. b1.objLiteral and b2.objLiteral refer to the same object:

    enter image description here

    and

    > b1.objLiteral === b2.objLiteral
      true
    

    To fix that, you have to create a new object of each instance. This is normally done by calling the “parent” constructor inside the “child” constructor:

    function ObjB() {
        ObjA.call(this);
        this.propB = 2;
    }
    

    ObjA.call(this) will call ObjA and within that function, this will refer to the argument passed to call [MDN], in this case the new instance of ObjB:

    enter image description here

    As you can see, objLiteral is now property of each instance:

    > b1.objLiteral === b2.objLiteral
      false
    

    To avoid this confusion in the first place, setting an instance of the parent constructor as the prototype of the child constructor should be avoided. What if the parent constructor expects instance specific arguments? What would you pass to ObjA in this case?

    It’s better to set the prototype of the child constructor to the prototype of the parent constructor (with one level of indirection) and call the parent constructor like above:

    function inherit(Child, Parent) {
        var Tmp = function() {};
        Tmp.prototype = Parent.prototype;
        Child.prototype = new Tmp();
        Child.prototype.constructor = Child;
    }
    
    inherit(ObjB, ObjA);
    

    Tmp to prevent extending Parent.prototype if you extend Child.prototype later on.


    Regarding propA:

    It “works”, because you are assigning a new value to it.

    Here are again the objects, after assigning properties to x.objLiteral and x.propA. You can see that the objects don’t have an own objLiteral property, but an own propA:

    enter image description here

    If instead you assign a new value to objLiteral, e.g. through

    b1.objLiteral = {hello: 42};
    

    b1 will now have its own property objLiteral, which shadows the inherited one:

    enter image description here

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

Sidebar

Related Questions

I have following code which works fine in iPhone application but in iPad I
I have following code which works for radio buttons but need to be changed
I have following code function Model(onChanged) { this.array = new Array(); this.onChanged = onChanged;
I have following code snippet: self.xmlHttpReq = new XMLHttpRequest(); self.xmlHttpReq.onreadystatechange = function() { if(self.xmlHttpReq.readyState
I have following code: Tools::Logger.Log(string(GetLastError()), Error); GetLastError() returns a DWORD a numeric value, but
I have following code in button click Response.Write(window.open('Contact.aspx')); The above code is working.but if
I have following code to send files to a FTP server. function FtpUploader( [string]$uri,
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following Code Block Which I tried to optimize in the Optimized section
I have following code in my application: // to set tip - photo in

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.