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?
问题描述
因此,我试图了解 o1.prototype = Object.create(o2.prototype)
和 o1.prototype = o2.prototype
之间的区别.
So I'm trying to understand the difference between o1.prototype = Object.create(o2.prototype)
and o1.prototype = o2.prototype
.
根据的答案>这个问题,前者通过obj1.prototype的原型将obj2.prototype设置为,但是我很难理解为什么要这么做(例如,新原型的原型只是Object.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.
例如,在以下代码中:
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);
o2.prototype.test
和 instance1.prototype.test
均打印"test"
.因此,将 o2
直接分配给 Object.create(o1.prototype)
或设置 o2
的值似乎并不重要 Object.create(o1.prototype)
的原型.
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)
.
另外,如果我对链接的问题的回答是正确的,如果 o1
为空(在本例中为空),则设置 o2 = o1
与设置设置 o2 = Object.create(o1)
相同,该设置也与
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;
这三个之间是否有显着差异?另外,如果 o2.prototype = Object.create(o1.prototype)
创建一个具有 o1.prototype
原型的 empty 对象,原型,如果 o1
的原型不为空,那么 o1
的原型的成员如何导入到 o2
的原型中?
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?
推荐答案
如果直接将 Parent.prototype
分配给孩子的原型,他们将都指向同一个对象.因此,如果添加仅适用于子类的方法,则父对象也将有权访问它们,因为 Parent.prototype === Child.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
示例:
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
如果您改用 Object.create(Animal.prototype)
,它会使用 [[[Prototype]]
(也是但已弃用的 __ proto __
)设置为 Anima.prototype
.因此,如果在 Human.prototype
上找不到任何方法,它将后退到 Animal.prototype
(在这种情况下, Eat
)
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)
当您访问 human.Eat()
时,首先该方法将直接在 human
对象下查找.如果找不到,将在其原型 Human.prototype
中进行搜索.
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;
因为那里没有找到 Eat
方法,所以将在 Human.prototype
的原型(即 Animal.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.
假设您要使用 human.hasOwnProperty('eyes')
.它经过与上述类似的链.如果在 human
对象, Human.prototype
或 Animal.prototype
对象上未找到 hasOwnProperty
,它将在 Object.prototpye
因为
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
有一个名为 hasOwnProperty
的方法,该方法将被执行
Object.prototype
has a method called hasOwnProperty
and that will be executed
这篇关于let o1.prototype = Object.create(o2.prototype)和o1.prototype = o2.prototype有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!