I thought I mastered prototypal inheritance but now setting object properties on derived objects work. Consider this code:
var com_mtag = {};
com_mtag.start = function(){
// creating myBase and setting a string property and an object-property
// both have a "prop" property set to "Base"
com_mtag.myBase = {};
com_mtag.myBase.prop = "Base";
com_mtag.myBase.obj = {prop: "Base"};
// Now derive myObj from myBase...
com_mtag.myObj = Object.create(com_mtag.myBase);
// modifying prop and obj.prop via the derived object
com_mtag.myObj.prop = "Derived";
com_mtag.myObj.obj.prop = "Derived";
};
The result is this: 1) both myBase and myObj have a property "prop" with the values "Base" and "Derived" respectively (as expected) 2) but myBase.obj and myObj.obj point to the same object which now has the value prop="Derived"
How does this work? I've learned that when setting object properties, the interpreter will not go down the prototype chain but create the property on the instance where the "set" was performed (it did this for the string property). But how did the interpreter handle setting the object property? It must have gone down the chain in order to locate the object. And did it create a new reference to this object within the derived object?
Hoping that I made myself clear (which I doubt when I read this but I cannot explain better), I would very much appreciate anyone to shed some light on this.