So I'm trying to understand the difference between o1.prototype = Object.create(o2.prototype)
and o1.prototype = o2.prototype
.
According to the answer to this question, the former sets obj2.prototype to by the prototype for obj1.prototype, but I'm having a hard time grasping why you would want that (the prototype of a new prototype for example is just Object.prototype since the prototype IS an Object without further inheritance). Furthermore, it doesn't seem to quite work the way the answer to that question suggests all the time.
In the following code, for example:
function o1(){}
o1.prototype.test = "test";
function o2(){}
o2.prototype = Object.create(o1.prototype);
let instance1 = Object.create(o1);
console.log(o2.prototype.test, instance1.prototype.test);
both o2.prototype.test
and instance1.prototype.test
print "test"
. So it doesn't seem to matter weather you assign the o2
directly to Object.create(o1.prototype)
or set the o2
's prototype to Object.create(o1.prototype)
.
Also, if I'm understanding this correctly, according to the answer in the linked question, if o1
is empty (which is it in this case) then setting o2 = o1
would be the same as setting setting o2 = Object.create(o1)
which would also be the same as
function o1(){};
function o2(){};
o2.prototype = o1.prototype;
Is there any significant difference between those three? Also, if o2.prototype = Object.create(o1.prototype)
creates an empty object with o1.prototype
's prototype as it's own prototype, if o1
's prototype is not empty, then how do the members of o1
's prototype get imported into o2
's prototype?