简体   繁体   English

了解原型 inheritance (Javascript)

[英]Understanding Prototypal inheritance (Javascript)

I'm trying to understand how prototypal inheritance works when using constructor functions in Javascript.我试图了解原型 inheritance 在 Javascript 中使用构造函数时的工作原理。 Take the following code for example:以下面的代码为例:

let Animal = function (name, species, birthYear) {
  this.name = name;
  this.species = species;
  this.birthYear = birthYear;
};

let spike = new Animal(`Spike`, `Dog`, 2000);

Animal.prototype.calculateAge = function () {
  let age = 2022 - this.birthYear;
  return age;
};

Animal.prototype.color = `brown`;

spike.calculateAge();
console.log(spike.color);
console.log(spike);

These last three lines are what I'm struggling to understand.最后三行是我难以理解的。 Please let me know if my understanding is misguided at any point in my explanation.如果我的解释在任何时候被误导,请告诉我。

I can call the calculateAge() method on the spike object because it inherited the method from the Animal prototype.我可以在尖峰 object 上调用 calculateAge() 方法,因为它继承了 Animal 原型的方法。 Similarly, I can log spikes color property to the console because the spike object inherited the color property from the Animal prototype.同样,我可以将尖峰颜色属性记录到控制台,因为尖峰 object 继承了动物原型的颜色属性。 When I inspect the spike object using console.log(), it has the properties of name, species, and birthYear that I defined, but there is also a prototype.当我使用 console.log() 检查尖峰 object 时,它具有我定义的名称、物种和出生年份的属性,但也有一个原型。 Does this mean that the spike object contains the prototype, or is this simply identifying the prototype from which it inherited the aforementioned method and property?这是否意味着尖峰 object 包含原型,或者这只是识别它继承了上述方法和属性的原型? Also, I don't really feel like I understand what this Animal prototype is.另外,我真的不觉得我理解这个 Animal 原型是什么。 I get that the code works and that's all well and good, but just what in the world is this Animal prototype thing?我知道代码可以工作,一切都很好,但是这个动物原型到底是什么? Is it its own object in and of itself?它本身是它自己的 object 吗?

Thank you for any explanation you can offer.感谢您提供的任何解释。

I think you should first deeply understand the object oriented paradigm behind Javascript.我想你应该首先深入了解Javascript背后的面向object的范式。

Javascript is a prototype based object oriented programming language. Javascript 是一种基于原型的 object 面向编程语言。

The operations are encoded in the prototype data structure in a prototype language, which is copied and updated at run time.操作以原型语言编码在原型数据结构中,并在运行时复制和更新。 However, a class is still the equivalence class for all objects with the same state space and methods when viewed abstractly.然而,当抽象地观察时,对于具有相同 state 空间和方法的所有对象,class 仍然是等价的 class。 You're effectively creating an element of a new equivalence class when you add a method to the prototype.当您向原型添加方法时,您实际上是在创建一个新的等价元素 class。

So, why are you doing that?那么,你为什么要这么做? Mostly because it results in a run-time system that is straightforward, logical, and elegant.主要是因为它产生了一个简单、合乎逻辑和优雅的运行时系统。 To create a new object or class, simply do a deep copy, which copies all of the data as well as the prototype data structure.要创建新的 object 或 class,只需进行深度复制,即复制所有数据以及原型数据结构。 Then you get inheritance and polymorphism for almost nothing: A method lookup always entails requesting a method implementation by name from a dictionary.然后你几乎没有得到 inheritance 和多态性:方法查找总是需要从字典中按名称请求方法实现。

To be more specific, JavaScript is a prototype-based object-oriented language, which means it doesn't have classes and instead defines behaviors using constructor functions, which can then be reused using the prototype.更具体地说,JavaScript 是一种基于原型的面向对象语言,这意味着它没有类,而是使用构造函数定义行为,然后可以使用原型重用。

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

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