Javascript 原型继承期间Object.create(Parent.prototype)与Object.create(Parent)之间的差异

Javascript 原型继承期间Object.create(Parent.prototype)与Object.create(Parent)之间的差异,javascript,Javascript,在典型的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

在典型的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”

根据定义,“Object.create()方法创建一个新对象,使用现有对象作为新创建对象的原型。”

我尝试了两种方法,console.log显示了两种方法的相同结果

A但没有澄清这一区别

var o = Object.create(Object.prototype); // same as var o = {};

您通常希望使用
Object.create(prototype)
从另一个对象创建对象,使用
Object.create(Class.prototype)
从函数创建对象

执行
Object.create(Class)
将使用该函数作为模板,这意味着只有“静态”字段将被传输到新对象
Object.create(Class.prototype)
将使用原型作为模板,因此您将能够获取使用
Class.prototype.field
声明的所有字段,您还可以通过构造函数获取静态字段,最后,在新对象上运行构造函数后,还将创建构造函数中声明的字段

函数Foo(){
//仅在运行构造函数后原型可见
this.test=函数(){};
}
Foo.prototype.bar=“bar”//原型可见
Foo.fooBar=“fooBar”//按类可见
var classFoo=Object.create(Foo);
var protoFoo=Object.create(Foo.prototype);
//fooBar是静态的,因此可以在classFoo中查看它
log(classFoo.fooBar);
//条形图已添加到原型中,无法查看
console.log(classFoo.bar);
//test在构造函数中声明,classFoo没有指向该构造函数的指针
日志(classFoo.test);
//此构造函数是所有类的基本构造函数
console.log(classFoo.constructor);
//fooBar已添加到构造函数中,因此不能以这种方式查看它
console.log(protoFoo.fooBar);
//但可以这样看
log(protoFoo.constructor.fooBar);
//这是Foo函数/构造函数
console.log(protoFoo.constructor);
//条形图被添加到原型中,以便可以查看
控制台日志(protoFoo.bar);
//测试尚未声明,因为构造函数尚未运行
日志(protoFoo.test);
//foo函数与protoFoo一起运行,如下所示
protoFoo.constructor();
//测试函数现在已添加到protoFoo中

日志(protoFoo.test)
Child.prototype=Object.create(Parent.prototype)
将与
Child.prototype=Object.create(Parent)
形成不同的原型链

使用
Child.prototype=Object.create(Parent.prototype)

foo的原型链是 Child.prototype-->Parent.prototype-->Object.prototype

简单解释

  • foo是从Child创建的,所以它的原型是Child.prototype
  • Child.prototype是从Object.create(Parent.prototype)创建的,所以它的原型是Parent.prototype
  • Parent.prototype是从Object创建的,所以它的原型是Object.prototye
  • Object.prototype的原型为空。这就是链条的末端
使用
Child.prototype=Object.create(父级)

foo的原型链将不同,如下所示 Child.prototype-->Parent-->Function.prototype-->Object.prototype

简单解释

  • foo是从Child创建的,所以它的原型是Child.prototype
  • Child.prototype是从Object.create(Parent)创建的,所以它的原型是Parent
  • 父项是从函数创建的,所以它的原型是Function.prototype
  • Function.prototype是从Object创建的,所以它的原型是Object.prototype
  • 如果Object.prototype为空,则为prototype。这就是链条的末端
在每种情况下,您都可以通过调用来进一步验证原型链。名称在Function.prototype中定义

console.log(foo.name);
对于第一种情况,由于Function.prototype不在prototype链中,您将得到未定义的结果,而在第二种情况下,您将得到“Parent”作为console.log的输出