将原型分配给Object.create原型有什么区别 [英] What is the difference between assigning prototype to Object.create prototype
本文介绍了将原型分配给Object.create原型有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我似乎不理解将一个构造函数原型分配给另一个构建器原型并使用Object.create之间的区别。
任何人都可以提供差异的示例吗?
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);
推荐答案
在您的示例中,对象.create
只创建一个模式(多余的)对象。如果你创建 new O1
和 new O2
,他们的继承图将是:
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:
这篇关于将原型分配给Object.create原型有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文