简体   繁体   English

原型继承

[英]prototypal inheritance

Here I am trying to understand few concepts of inheritance in javascript.I have created person class and trying to inherit it in Customer class.在这里,我试图了解 javascript 中继承的几个概念。我创建了 person 类并尝试在 Customer 类中继承它。

        var Person = function(name) {
            this.name = name;
        };

        Person.prototype.getName = function() {
            return this.name;
        };

        Person.prototype.sayMyName = function() {
            alert('Hello, my name is ' + this.getName());
        };


        var Customer = function(name) {
            this.name = name;
        };

        Customer.prototype = new Person();


        var myCustomer = new Customer('Dream Inc.');
        myCustomer.sayMyName();

Every time a new object gets created ,javascript engine basically calls prototype's constructor.每次创建新对象时,javascript 引擎基本上都会调用原型的构造函数。 here I am trying to understand few things:在这里,我试图了解一些事情:

if Customer prototype is referring to Person object.So creation of new Customer object should contain only Person property/Method not the Customer property/Method.How Customer property get attached to the new Customer object (myCustomer)?如果 Customer 原型指的是 Person 对象。所以新 Customer 对象的创建应该只包含 Person 属性/方法而不是 Customer 属性/Method。Customer 属性如何附加到新的 Customer 对象 (myCustomer)?

Am I missing some javascript concept here?我在这里错过了一些 javascript 概念吗?

Here I am trying to understand few concepts of inheritance in javascript.I have created person class and trying to inherit it in Customer class.在这里,我试图了解 javascript 中继承的几个概念。我创建了 person 类并尝试在 Customer 类中继承它。

Well then you sure have lost your way quickly.那么你肯定很快迷路了。 There are no classes in prototypal inheritance.原型继承中没有类。
That's the whole point!这就是重点! :-) :-)


Using .prototype and new Function() syntax isn't a very explicit way to leverage prototypal inheritance.使用.prototypenew Function()语法并不是利用原型继承的一种非常明确的方式。

Consider using Object.create instead of new - this way allows you to directly say which object is supposed to be which's prototype.考虑使用Object.create而不是new - 这种方式允许您直接说出哪个对象应该是哪个原型。 It's more straightforward and you're likely to grasp the idea of prototypes faster this way.它更直接,您可能会以这种方式更快地掌握原型的想法。

Also, if you want to have longer prototype chains, then this method will certainly be more comfortable to use.另外,如果你想拥有更长的原型链,那么这种方法使用起来肯定会更舒服。

You defined Person and Customer , and then set Customer 's prototype to a Person instance.您定义了PersonCustomer ,然后将Customer的原型设置为Person实例。 So, your chain looks like this:所以,你的链看起来像这样:

myCustomer              (a Customer instance)
  Customer prototype    (a Person instance)
    Person prototype    (a plain object)

Indeed, myCustomer is a Customer instance as it has Customer 's prototype in its chain.实际上, myCustomer是一个Customer实例,因为它的链中有Customer的原型。 It's not the same thing as a direct Person instance.它与直接的Person实例不同。 The latter would not have Customer 's prototype in the chain.后者在链中不会有Customer的原型。

if Customer prototype is referring to Person object and so on creation new Customer object should contain only Person property/Method not Customer property/Method.如果 Customer 原型指的是 Person 对象等等,那么创建新的 Customer 对象应该只包含 Person 属性/方法而不是 Customer 属性/方法。

Yes.是的。 Your newly created Customer object inherits from the Person instance in Customer.prototype .新创建的Customer从对象继承Person的情况下Customer.prototype As you have neither added properties to that prototype object, nor create properties to your instance in the Customer constructor, your customer contains only the person's properties.由于您既没有向该原型对象添加属性,也没有在Customer构造函数中为您的实例创建属性,因此您的客户仅包含此人的属性。

How Customer property get attached to the new Customer object (myCustomer)? Customer 属性如何附加到新的 Customer 对象 (myCustomer)?

Do it in the constructor function:在构造函数中执行:

function Customer (){
    this.customer_property = someValue;
}

This code is executed each time an instance is created via new .每次通过new创建实例时都会执行此代码。 If you don't need that, you can just add the methods etc. to the Customer.prototype object.如果您不需要它,您可以将方法等添加到Customer.prototype对象中。

Also have a look at What is the reason [not] to use the 'new' keyword here?也看看什么是[不]在这里使用'new'关键字的原因? - you should not instantiate a Person for the prototype object - why should all customers inherit its name etc? - 你不应该为原型对象实例化一个 Person - 为什么所有客户都应该继承它的名字等等? Use

function Customer(name) {
    Person.call(this, name); // apply Person constructor on this instance
                             // for creating own properties (and privileged methodes)
    // custom Customer code
}
Customer.prototype = Object.create(Person.prototype);
Customer.prototype.someMethod = … // Customer-specific prototype things

I think the confusion lies in saying "javascript engine basically calls prototype's constructor".我认为混淆在于说“javascript 引擎基本上调用原型的构造函数”。 For a new instance of your Customer, Javascript will create a new object, set the prototype to the Person object you've designated, then call your Customer constructor function to customize the object (this is the illusion of "class" in JS).对于您的 Customer 的新实例,Javascript 将创建一个新对象,将原型设置为您指定的 Person 对象,然后调用您的 Customer 构造函数来自定义该对象(这是 JS 中“类”的错觉)。 There's no re-calling of the prototype constructor function.没有重新调用原型构造函数。 So, your fields/methods for Customer are set, in your Customer constructor.因此,在 Customer 构造函数中设置了 Customer 的字段/方法。

When a customer object has a method/field referenced after construction, the javascript engine will try to find it in the fields/methods that were set via your constructor (or in code manipulating the object downstream of the constructor call).当客户对象在构造后引用了方法/字段时,javascript 引擎将尝试在通过构造函数设置的字段/方法(或在构造函数调用下游操作对象的代码中)中找到它。 If it can't, it will look to the prototype object attached to find the method/field.如果不能,它将查找附加的原型对象以查找方法/字段。 In this way are both the Customer fields/methods available and the "superclass" fields/methods.通过这种方式,客户字段/方法和“超类”字段/方法都可用。

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

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