"I figured to do it makes each obj dependent on the other"
As Kyle explains when two objects are [[Prototype]]
linked, they aren't really
dependent on each other; instead they are individual object. You're linking one
object to the other with a [[Prototype]]
linkage which you can change anytime you wish. If you take two [[Prototype]]
linked objects created through OLOO style as being dependent on each other, you should also think the same about the ones created through constructor
calls.
var foo= {},
bar= Object.create(foo),
baz= Object.create(bar);
console.log(Object.getPrototypeOf(foo)) //Object.prototype
console.log(Object.getPrototypeOf(bar)) //foo
console.log(Object.getPrototypeOf(baz)) //bar
Now think for a second do you think of foo
bar
and baz
as being dependent on each-other?
Now let's do the same this 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)) //Foo.prototype
console.log(Object.getPrototypeOf(Foo.prototype)) //Object.prototype
console.log(Object.getPrototypeOf(bar)) //Bar.prototype
console.log(Object.getPrototypeOf(Bar.prototype)) //Foo.prototype
console.log(Object.getPrototypeOf(baz)) //Baz.prototype
console.log(Object.getPrototypeOf(Baz.prototype)) //Bar.prototype
The only difference b/w the latter and the former code is that in the latter one
foo
, bar
, baz
bbjects are linked to each-other through arbitrary objects
of their constructor
function (Foo.prototype
, Bar.prototype
, Baz.prototype
) but in the former one (OLOO
style) they are linked directly. Both ways you're just linking foo
, bar
, baz
with each other, directly in the former one and indirectly in the latter one. But, in both the cases the objects are independent of each-other because it isn't really like an instance of any class which once instantiated, can't be made to inherit from some other class. You can always change which object an object should delegate too.
var anotherObj= {};
Object.setPrototypeOf(foo, anotherObj);
So they're all independent of each-other.
" I was hoping OLOO
would solve the issue in which each object knows
nothing about the other."
Yes that's indeed possible-
Let's use Tech
as an utility object-
var Tech= {
tag: "technology",
setName= function(name) {
this.name= name;
}
}
create as many objects as you wish linked to 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 html
, css
, js
objects are connected to each-other? No, they aren't. Now let's see how we could've done that with constructor
function-
function Tech() { }
Tech.prototype.tag= "technology";
Tech.prototype.setName= function(name) {
this.name= name;
}
create as many objects as you wish linked to Tech.proptotype
-
var html= new Tech(),
css= new Tech(),
js= new 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.isPrototypeOf(css); //false
Tech.prototype.isPrototypeOf(html); //true
Tech.prototype.isPrototypeOf(css); //true
Tech.prototype.isPrototypeOf(js); //true
How do you think these constructor
-style Objects (html
, css
, js
)
Objects differ from the OLOO
-style code? In fact they serve the same purpose. In OLOO
-style one objects delegate to Tech
(delegation was set explicitly) while in constructor
-style one objects delegate to Tech.prototype
(delegation was set implicitly). Ultimately you end up linking the three objects, having no linkage with each-other, to one object, directly using OLOO
-style, indirectly using constructor
-style.
"As is, ObjB has to be created from ObjA.. Object.create(ObjB) etc"
No, ObjB
here is not like an instance (in classical-based languages) of any class
ObjA
. It sould be said like objB
object is made delegate to ObjA
object at it's creation
time". If you used constructor, you would have done the same 'coupling', although indirectly by making use of .prototype
s.