let o1.prototype = Object.create(o2.prototype) 和 o1.prototype = o2.prototype 有什么区别? [英] What is the difference let o1.prototype = Object.create(o2.prototype) and o1.prototype = o2.prototype?

查看:24
本文介绍了let o1.prototype = Object.create(o2.prototype) 和 o1.prototype = o2.prototype 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图理解 o1.prototype = Object.create(o2.prototype)o1.prototype = o2.prototype 之间的区别.>

根据的回答这个问题,前者通过 obj1.prototype 的原型将 obj2.prototype 设置为,但我很难理解你为什么想要那个(例如,一个新原型的原型就是 Object.prototype因为原型是一个没有进一步继承的对象).此外,它似乎并不像该问题的答案一直暗示的那样有效.

以下面的代码为例:

函数 o1(){}o1.prototype.test = "测试";函数 o2(){}o2.prototype = Object.create(o1.prototype);让 instance1 = Object.create(o1);console.log(o2.prototype.test, instance1.prototype.test);

o2.prototype.testinstance1.prototype.test 都打印 "test".因此,将 o2 直接分配给 Object.create(o1.prototype) 或设置 o2 的天气似乎并不重要Object.create(o1.prototype) 的原型.

另外,如果我理解正确,根据链接问题中的答案,如果 o1 为空(在这种情况下就是这样),则设置 o2 = o1 将与设置设置相同 o2 = Object.create(o1) 也将与

相同

function o1(){};函数 o2(){};o2.prototype = o1.prototype;

这三者之间有什么显着区别吗?此外,如果 o2.prototype = Object.create(o1.prototype) 使用 o1.prototype 的原型创建一个 对象作为它自己的原型原型,如果o1的原型不为空,那么o1的原型的成员如何导入到o2的原型中?

解决方案

如果您直接将 Parent.prototype 分配给孩子的原型,它们将指向同一个对象.所以,如果你添加一个只适用于子类的方法,Parent 对象也可以访问它们,因为 Parent.prototype === Child.prototype

示例:

function Animal() {};Animal.prototype.Eat = function() {console.log("吃东西")}函数人(){};Human.prototype = Animal.prototype;//都指向同一个对象Human.prototype.Drive = function() {console.log("驾驶")}var 动物 ​​= 新动物();var human = new Human();动物吃();人类吃();动物驱动器();//动物不应该开车人类驱动器();console.log("animal instanceof Human: ",animal instanceof Human)//true

如果您改用 Object.create(Animal.prototype),它会创建一个带有 [[Prototype]] 的新对象(同样,但不推荐使用,__proto__) 设置为 Anima.prototype.因此,如果在 Human.prototype 上找不到任何方法,它会回退Animal.prototype(在这种情况下 Eat)

function Animal() {};Animal.prototype.Eat = function() {console.log("吃东西")}函数人(){};Human.prototype = Object.create(Animal.prototype)Human.prototype.constructor = 人类;//更新构造器Human.prototype.Drive = function() {console.log("驾驶")}var 动物 ​​= 新动物;var 人类 = 新人类;动物吃();人类吃();人类驱动器();尝试 {//这会抛出一个错误,因为 Animal.prototype 没有 Drive 方法动物驱动器();} 抓住 {console.log("动物不能开车")}console.log("animal instanceof Animal: ",animal instanceof Animal)//trueconsole.log("animal instanceof Human: ",animal instanceof Human)//falseconsole.log("human instanceof Animal: ", human instanceof Animal)//trueconsole.log("human instanceof Human: ", human instanceof Human)//true控制台日志(动物.构造函数)console.log(human.constructor)

当你访问human.Eat()时,首先该方法会直接在human对象下查找.如果没有找到,将在其原型中搜索,即 Human.prototype.

Object.getPrototypeOf(human) === Human.prototype;

由于在那里没有找到Eat方法,该方法将在Human.prototype的原型中查找,即Animal.prototype

Object.getPrototypeOf(Human.prototype) === Animal.prototype

方法在这里找到,就会执行.

<小时>

假设您想使用 human.hasOwnProperty('eyes').它通过与上面类似的链.如果在 human 对象、Human.prototypeAnimal.prototype 上找不到 hasOwnProperty,它会检查 humancode>Object.prototpye 因为

Object.getPrototypeOf(Animal.prototype) === Object.prototype

Object.prototype 有一个名为 hasOwnProperty 的方法,它将被执行

So I'm trying to understand the difference between o1.prototype = Object.create(o2.prototype) and o1.prototype = o2.prototype.

According to the answer to this question, the former sets obj2.prototype to by the prototype for obj1.prototype, but I'm having a hard time grasping why you would want that (the prototype of a new prototype for example is just Object.prototype since the prototype IS an Object without further inheritance). Furthermore, it doesn't seem to quite work the way the answer to that question suggests all the time.

In the following code, for example:

function o1(){}
o1.prototype.test = "test";
function o2(){}
o2.prototype = Object.create(o1.prototype);
let instance1 = Object.create(o1);
console.log(o2.prototype.test, instance1.prototype.test);

both o2.prototype.test and instance1.prototype.test print "test". So it doesn't seem to matter weather you assign the o2 directly to Object.create(o1.prototype) or set the o2's prototype to Object.create(o1.prototype).

Also, if I'm understanding this correctly, according to the answer in the linked question, if o1 is empty (which is it in this case) then setting o2 = o1 would be the same as setting setting o2 = Object.create(o1) which would also be the same as

function o1(){};
function o2(){};
o2.prototype = o1.prototype;

Is there any significant difference between those three? Also, if o2.prototype = Object.create(o1.prototype) creates an empty object with o1.prototype's prototype as it's own prototype, if o1's prototype is not empty, then how do the members of o1's prototype get imported into o2's prototype?

解决方案

If you directly assign Parent.prototype to a child's prototype, they'll both be pointing to the same object. So, if you add a method, which only applies to the child class, Parent objects will also have access to them because Parent.prototype === Child.prototype

Example:

function Animal() {};
Animal.prototype.Eat = function() {
  console.log("Eating")
}

function Human() {};
Human.prototype = Animal.prototype; // both point to the same object

Human.prototype.Drive = function() {
  console.log("Driving")
}

var animal = new Animal();
var human = new Human();

animal.Eat();
human.Eat();

animal.Drive(); // Animals shouldn't be driving
human.Drive();

console.log("animal instanceof Human: ", animal instanceof Human) // true

If you use Object.create(Animal.prototype) instead, it creates a new object with the [[Prototype]] (also, but deprecated, __proto__) set to Anima.prototype. So, if any methods are not found on Human.prototype, it will fall back to Animal.prototype (In this case Eat)

function Animal() {};
Animal.prototype.Eat = function() {
  console.log("Eating")
}

function Human() {};
Human.prototype = Object.create(Animal.prototype)
Human.prototype.constructor = Human; // update the constrcutor

Human.prototype.Drive = function() {
  console.log("Driving")
}

var animal = new Animal;
var human = new Human;

animal.Eat();
human.Eat();
human.Drive();

try {
   // This will throw an error because Animal.prototype doesn't have a Drive method
  animal.Drive();
} catch {
  console.log("Animals can't drive")
}

console.log("animal instanceof Animal: ", animal instanceof Animal) // true
console.log("animal instanceof Human: ", animal instanceof Human) // false
console.log("human instanceof Animal: ", human instanceof Animal) // true
console.log("human instanceof Human: ", human instanceof Human) // true

console.log(animal.constructor)
console.log(human.constructor)

When you access, human.Eat(), first the method will looked up directly under the human object. If not found, this will be searched in its prototype which is Human.prototype.

Object.getPrototypeOf(human) === Human.prototype;

Since, Eat method is not found there, the method will be looked inside the prototype of Human.prototype which is Animal.prototype

Object.getPrototypeOf(Human.prototype) === Animal.prototype

The method is found here and it will be executed.


Let's say you want to use human.hasOwnProperty('eyes'). It goes through the similar chain as above. If hasOwnProperty is not found on human object, Human.prototype or Animal.prototype, it will check inside Object.prototpye because

Object.getPrototypeOf(Animal.prototype) === Object.prototype

Object.prototype has a method called hasOwnProperty and that will be executed

这篇关于let o1.prototype = Object.create(o2.prototype) 和 o1.prototype = o2.prototype 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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