What is the difference between assigning prototype

2019-03-06 03:40发布

问题:

I don't seem to understand the difference between assigning one constructor prototype to an other, and using Object.create. can anyone provide an example of the difference?

I've created this inheritance example which provides the same result for both cases:

function MyObject() {
}

function O1() {
   MyObject.call(this);
}

O1.prototype = MyObject.prototype;

function O2() {
    MyObject.call(this);
}

O2.prototype = Object.create(MyObject.prototype);

回答1:

In your example, Object.create just creates one mode (superfluous) object. If you create new O1 and new O2, their inheritance diagram will be this: