"I decided to do this so that each object depended on another"
As Kyle explains, when two objects are connected [[Prototype]] , they are not really dependent on each other; instead, they are an individual object. You link one object to another using the [[Prototype]] link, which you can change at any time. If you take two [[Prototype]] related objects created in OLOO style as dependent on each other, you should also think of those created through constructor calls.
var foo= {}, bar= Object.create(foo), baz= Object.create(bar); console.log(Object.getPrototypeOf(foo))
Now think, do you think foo bar and baz depend on each other?
Now let's do the same constructor style code -
function Foo() {} function Bar() {} function Baz() {} Bar.prototype= Object.create(Foo); Baz.prototype= Object.create(Bar); var foo= new Foo(), bar= new Bar(). baz= new Baz(); console.log(Object.getPrototypeOf(foo))
The only difference b / w of the last and previous code is that in the last foo , bar , baz bbjects are connected to each other through arbitrary objects of their constructor function ( Foo.prototype , Bar.prototype , Baz.prototype ), but in the first ( OLOO style) they are directly related. In both ways, you simply bind foo , bar , baz each other, directly in the first and indirectly in the last. But in both cases, the objects are independent of each other, because it does not look like an instance of any class that, after creating the instance, cannot be inherited from any other class. You can always change which object should delegate the object.
var anotherObj= {}; Object.setPrototypeOf(foo, anotherObj);
Therefore, they are all independent of each other.
"I was hoping OLOO would solve a problem in which every object knows nothing about the other."
Yes, what is really possible -
Let me use Tech as a utility object -
var Tech= { tag: "technology", setName= function(name) { this.name= name; } }
create as many objects that you want to associate with Tech -
var html= Object.create(Tech), css= Object.create(Tech), js= Object.create(Tech); Some checking (avoiding console.log)- html.isPrototypeOf(css); //false html.isPrototypeOf(js); //false css.isPrototypeOf(html); //false css.isPrototypeOf(js); //false js.isPrototypeOf(html); //false js.isPrototypwOf(css); //false Tech.isPrototypeOf(html); //true Tech.isPrototypeOf(css); //true Tech.isPrototypeOf(js); //true
Do you think that html , css , js objects are related to each other? No, it is not. Now let's see how we could do this using the constructor function -
function Tech() { } Tech.prototype.tag= "technology"; Tech.prototype.setName= function(name) { this.name= name; }
create as many objects you want to associate with Tech.proptotype -
var html= new Tech(), css= new Tech(), js= new Tech();
Some check (excluding console.log) -
html.isPrototypeOf(css); //false html.isPrototypeOf(js); //false css.isPrototypeOf(html); //false css.isPrototypeOf(js); //false js.isPrototypeOf(html); //false js.isPrototypeOf(css); //false Tech.prototype.isPrototypeOf(html); //true Tech.prototype.isPrototypeOf(css); //true Tech.prototype.isPrototypeOf(js); //true
Do you think these constructor style objects ( html , css , js ) Objects are different from OLOO style code? In fact, they serve the same purpose. In OLOO build one object to delegate to Tech (delegation was specified explicitly), and in constructor - create one object to delegate to Tech.prototype (delegation was implicitly specified). In the end, you have a relationship between three objects that are not related to each other, with one object, directly using the OLOO style, indirectly using the constructor style.
"As is, ObjB must be created from ObjA .. Object.create (ObjB), etc.
No, ObjB is not like an instance (in classical languages) of any ObjA class. We can say that how an ObjB object is passed to the ObjA object when it is created time . " If you used the constructor, you would have made the same“ link ”, albeit indirectly, using .prototype s.