What is the difference between assigning prototype

2019-03-06 03:36发布

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条回答
淡お忘
2楼-- · 2019-03-06 03:56

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:

enter image description here

查看更多
登录 后发表回答