【发布时间】:2019-01-25 07:59:13
【问题描述】:
在典型的 JavaScript 继承中,我们将 Parent.prototype 传递给 Object.create。
function Parent() {};
function Child() {
Parent.call(this);
}
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
var foo = new Child();
调用 Object.create(Person) 和 Object.create(Parent.prototype) 有区别吗?
包括下面链接的 MDN 文章在内的许多教程都传递了“Parent.prototype”,而不仅仅是“Parent”。
根据MDN 定义“Object.create() 方法创建一个新对象,使用现有对象作为新创建对象的原型。”
我尝试了两种方法,console.log 显示两种方法的结果相同。
similar question was answered here,但没有说明这种区别。
var o = Object.create(Object.prototype); // same as var o = {};
【问题讨论】:
标签: javascript