简体   繁体   English

理解Javascript中的经典继承

[英]Understanding Classical Inheritance in Javascript

I don't entirely understand how this works: 我不完全理解这是如何工作的:

function inherit(C,P) {
  var F = function () {};
  F.prototype = P.prototype;
  C.prototype = new F();
}

function Parent(){};
Parent.prototype.species = "human";
function Child(){};
inherit(Child, Parent);
var kid = new Child();
Parent.prototype.talk = function () {return "Hello there!"};

How does the kid object have the talk function? kid对象如何具有talk功能? Doesn't the inherit function only give the Child "class" the prototype of the parent at the time you call the inherit function? 继承函数是否仅在您调用继承函数时将Child“class”赋予父类的原型? How is it able to update it? 它是如何更新的?

The inherit function does this: inherit函数执行此操作:

function inherit(C, P) {
  var F = function () {};    // Defines a new constructor function.
  F.prototype = P.prototype; // Sets `F.prototype` to *be an alias* of `P.prototype`
  C.prototype = new F();     // Sets `C.prototype` to *be an instance* of `F`
}

If you change C by Child , P by Parent and F by anonymous class . 如果您更改C by ChildP更改为ParentF更改为anonymous class It will render something like this: 它将呈现如下内容:

Child.prototype is an instance of anonymous class , which shares the same prototype as Parent . Child.prototypeanonymous class一个实例,它与Parent共享相同的原型。

This makes this statement to be true: Child.prototype.__proto__ === Parent.prototype . 这使得这个陈述成立: Child.prototype.__proto__ === Parent.prototype

So, any changes you make to Parent.prototype will be seen by the instance of the anonymous class in Child.prototype . 所以,你做任何修改Parent.prototype将通过实例可以看出anonymous classChild.prototype And, consequently, this makes it acessible to Child instances too. 因此,这也使得Child实例也可以访问它。

But, the inverse is not true! 但是,反过来是不正确的! For example, if you add properties to Child.prototype , you are only changing the instance of the anonymous class and it will not be visible to instances of Parent . 例如,如果向Child.prototype添加属性,则只更改anonymous class的实例,并且它对Parent实例不可见。

The basic principle here is that in Javascript when you set one object to equal another, you are only creating a reference to the original object and not a duplicate of that object. 这里的基本原则是,在Javascript中,当您将一个对象设置为等于另一个时,您只是创建对原始对象的引用而不是该对象的副本。 Therefore any changes to the original object will also apply to the link you created. 因此,对原始对象的任何更改也将应用于您创建的链接。

Here's a simpler version that follows the same principle: 这是一个更简单的版本遵循相同的原则:

var parent = {
    "speak": "hello"
}

var child = parent;
parent.shout = "HELLO!"

alert(child.shout); // Will popup 'HELLO!'

So when you're setting your Child.prototype to equal Parent.prototype, Child.prototype is now just a reference to the Parent.prototype object, so any changes you make to either will apply to both. 因此,当您将Child.prototype设置为等于Parent.prototype时,Child.prototype现在只是对Parent.prototype对象的引用,因此您对其中任何一项所做的任何更改都将适用于两者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM