Object.create 与 new 在原型继承方面 [英] Object.create vs new in terms of prototype inheritance

查看:43
本文介绍了Object.create 与 new 在原型继承方面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了很多关于它,但我找不到任何直接的答案

I have searched a lot about it but I couldn't find any direct answer to it

为什么我们不能访问使用 Object.create() 创建的对象的原型属性/方法,但我们可以使用 new 关键字?

Why we can't access prototype property/method of an object created using Object.create() but we can using new keyword?

让我们考虑如下构造函数Greeting:

Let us consider a constructor function Greeting as follows:

function Greeting() {   
}
Greeting.prototype.byeBye = function(){
    console.log("Have a good day!")
}

现在我使用 Object.create() 方法从这个构造函数创建一个对象

Now I create an object from this constructor using Object.create() method

var Stanley = Object.create(Greeting)
console.log(Stanley.byeBye())

Output -  Error: Stanley.byeBye is not a function

这意味着不能使用 Stanley 对象直接访问 byeBye() 函数

Which means byeBye() function cannot be accessed directly using Stanley object

如果您在控制台中检查 Stanley 对象,则会发现以下结构:

If you check for Stanley object in console, then you will find the following structure:

但是如果我们使用new关键字来创建对象,那么我们可以直接访问byeBye()原型函数

However if we use the new keyword to create the object, then we can access byeBye() prototype function directly

var Hamley = new Greeting()
console.log(Hamley.byeBye())

Output: Have a good day!

在控制台中检查 Hamley 对象的结构时,我们得到如下:

On checking the structure of Hamley object in console, we get it like this:

我的疑问是为什么我们不能访问使用 Object.create() 创建的对象的原型属性/方法,但我们可以使用 new 关键字.

My doubt is that why we can't access prototype property/method of an object created using Object.create() but we can using the new keyword.

另一个疑问是,为什么在 Object.create() 的情况下,原型方法 byeBye() 属于在 __proto__ 中调用的单独属性作为prototype".而在 new 的情况下,byeBye() 函数可直接在 __proto__ 下使用.

Another doubt is that why in case of Object.create(), prototype method byeBye() comes under seperate property in __proto__ called as 'prototype'. Whereas in case of new, byeBye() function is directly available under __proto__.

注意:请检查链接以打开与第二个疑问相关的图像

Note: Please check the links to open the images related to second doubt

感谢所有回答这篇文章的人!

Thanks to anyone who answers to this post!

推荐答案

您提供给 Object.create 的对象应该是您希望新对象具有的原型.您正在传入构造函数,因此使构造函数成为对象的原型.您可能打算使用 new 将使用的相同对象,该对象位于构造函数的 prototype 属性上.

The object you give to Object.create should be the prototype you want the new object to have. You're passing in the constructor function, so that's making the constructor function the prototype of the object. You probably meant to use the same object new would use, which is on the prototype property of the constructor.

所以代替:

var Stanley = Object.create(Greeting)

你想要

var Stanley = Object.create(Greeting.prototype)
// ---------------------------------^^^^^^^^^^

现场示例:

function Greeting() {   
}
Greeting.prototype.byeBye = function(){
    console.log("Have a good day!");
}

var Stanley = Object.create(Greeting.prototype);
Stanley.byeBye();

这篇关于Object.create 与 new 在原型继承方面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆