If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Shared Prototype Objects

Hi everybody,

I'm having a little problem with JavaScript and Object Prototypes. I have been working with simple uses of the Object but as I get into more complex scenarios, like inheritance, I seem to be missing something basic about the whole Constructor, Prototype, and Object relationship.

My specific question is - How do you create an Object that points back to a Prototype Object created earlier for another Object using the same Constructor. For instance, sorry this is complex:

If I have a simple hierarchy of Objects where OneObj is at the top and TwoObj is in the middle and ThreeObj is at the bottom. In this scenario OneObj is parent to TwoObj which in turn is parent to ThreeObj. Then I want to create a Prototype Chain like so:

At the start of the script I create an Object 'objA' using just two of our three objects. At this point all is good. Then I want to create a slightly different Object introducing a new Constructor (TwoObj) that I add to the Prototype Inheritance

Here is where I have a problem. I would expect that objC and objA would share Prototypes (point to the same Prototype Object). But when I added a new property to objC like:

objC.constructor.prototype.propNew = 18;

It only shows up in objC's Prototype and not objA's. I thought this might be because I was setting the ThreeObj.prototype = new OneObj() and the new OneObj was an new instantiation so perhaps it cannot be shared! So the next test was to use the prototype from objA. I did this as follows:

ok, this is kinda complex so pardon the multi-quote, which typically is used to rudely take pot shots at other posters...

Originally Posted by throwcode

Hi rnd me,
You don't seem to have addressed my question. When I add a new property to objC's Constructor's Prototype it was not visible in objA, which I thought would share Prototypes.

objC has a different prototype than objA; both are distinct instances of OneObj in your orig post. you pulled a switcharoo on the proto between the instantiations. if an instance inherits something through it's constructor's prototype, and you re-assign the contructor's prototype and create a new instance, you're tacking on the new prototype to the newer object, not the old one.

a side-effect test of this would be if ".prototype=new " appears between the two instantiations. Since your code is partial in your replay and typoed in your orig post, i'm not 100% sure what your current setup is, but if you re-assign a property, you re-assign a property, no matter where it ends up poking its head.

Essentially, you're one level too high, one dot to the left, or however you want to think of it. you can modify an existing prototype for instant system-wide inheritance, but you cannot replace it completely and get the same satisfaction.

Interestingly, you can .call() a constructor to a prototype so that all existing instances will get the own properties of that constructor's instance, but that's not 100% the same as what you're after..

this is typically a lot faster (execution-wise) than iterating properties to merge objects or fake a more formalized deep inheritance.

Originally Posted by throwcode

I understand why the Constructor properties in your example add to 6. Though I see you have left out the relinking of the Constructor. Is that the way it should be done?

once you have a 3rd nesting of a constructor, the "constructor" property is of little use. Actually, other than for sniffing native types (Array vs Object vs Date), it's pretty much useless anyway, and has a number of limitations and flaws. it would be great if it worked 100% in custom OOP getups, but it doesn't, so you should use other means to navigate.

instanceof of will identify the instance with any sub-group of inherited properties (from the nested prototypes), whereas your "constructor" property just hits the most recent.

another issue with setting the "constructor" property manually like that is that for(x in new ob) will dredge up that property, whereas in a native object instance like for(x in new String), "constructor" will not be an own property.

functions don't show up in JSON.stringify, so the extra properties might not hurt your project, but be aware of them...

anyway, our code is similar, i just didn't manually set the constructor property, which we don't need to do. since it can be re-assigned, and it's relative, it can be confusing to work with and explicitly comparing an object to any constructor in it's chain is fool-proof.

feel free to post you current whole code if that doesn't make sense and we'll see if i or someone else can explain it better for your exact issue.

Last edited by rnd me; 04-05-2013 at 05:32 AM.

Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

Users who have thanked rnd me for this post:

First let me thank you for all the time you've taken so far. I admit that I am still noodling over your comments. I'm afraid there is no convenient source to review. When I ran into this problem this is the test code I created to walk through the various scenarios. It was meant to be simpler!

But thanks to you and what I understand so far I have a partial solution.

By removing the reset of the Prototype's 'constructor' property I can now see the new properties and methods being added to what I believe are the correct Prototype levels for the right Object Instantiations. Thanks for that.

However, this has allowed me to look deeper and discover another result that I cannot explain. The following code gives me two different results: