基于原型的基于类的继承 [英] prototype based vs. class based inheritance

查看:122
本文介绍了基于原型的基于类的继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript中,每个对象都是一个实例和一个类。要做继承,可以使用任何对象实例作为原型。

In JavaScript, every object is at the same time an instance and a class. To do inheritance, you can use any object instance as a prototype.

在Python,C ++等中,有类和实例作为单独的概念。为了做继承,你必须使用基类来创建一个新的类,然后可以使用它来生成派生的实例。

In Python, C++, etc.. there are classes, and instances, as separate concepts. In order to do inheritance, you have to use the base class to create a new class, which can then be used to produce derived instances.

为什么JavaScript在这个方向(基于原型的对象方向)?相对于传统的基于类的OO,基于原型的OO的优点(和缺点)是什么?

Why did JavaScript go in this direction (prototype-based object orientation)? what are the advantages (and disadvantages) of prototype-based OO with respect to traditional, class-based OO?

推荐答案

大约在这里有一百个术语问题,大多是围绕着某人(不是你),试图让自己的想法听起来像是最好的。

There are about a hundred terminology issues here, mostly built around someone (not you) trying to make their idea sound like The Best.

所有面向对象的语言都需要能够处理具有以下几个概念:

All object oriented languages need to be able to deal with several concepts:


  1. 数据封装以及关于数据的关联操作,不同地称为数据成员和成员函数,或作为数据和方法,除其他外,继承,这些对象的能力就像其他一组对象,除了这些更改外,还有另外一些对象

  2. (许多形状),其中对象决定要运行哪些方法,以便您可以依靠语言正确路由请求。

现在,至于比较:

第一件事是整个类vs原型问题。这个想法最初在Simula中开始,在这里使用基于类的方法,每个类都表示一组共享相同状态空间的对象(读取可能值)和相同的操作,从而形成一个等价类。如果你回头看Smalltalk,因为你可以打开一个类并添加方法,这与Javascript可以做的一样。

First thing is the whole "class" vs "prototype" question. The idea originally began in Simula, where with a class-based method each class represented a set of objects that shared the same state space (read "possible values") and the same operations, thereby forming an equivalence class. If you look back at Smalltalk, since you can open a class and add methods, this is effectively the same as what you can do in Javascript.

稍后的OO语言想要能够使用静态类型检查,所以我们在编译时得到了固定类集的概念。在开放式版本中,您有更大的灵活性;在较新版本中,您可以在编译器中检查某些正确性,否则将需要进行测试。

Later OO languages wanted to be able to use static type checking, so we got the notion of a fixed class set at compile time. In the open-class version, you had more flexibility; in the newer version, you had the ability to check some kinds of correctness at the compiler that would otherwise have required testing.

在基于类的语言中,复制发生在编译时。在原型语言中,操作存储在原型数据结构中,它在运行时被复制和修改。尽管如此,一个类仍然是共享相同状态空间和方法的所有对象的等价类。当您向原型添加方法时,您有效地创建了一个新的等价类的元素。

In a "class-based" language, that copying happens at compile time. In a prototype language, the operations are stored in the prototype data structure, which is copied and modified at run time. Abstractly, though, a class is still the equivalence class of all objects that share the same state space and methods. When you add a method to the prototype, you're effectively making an element of a new equivalence class.

现在,为什么这样做?主要是因为它在运行时产生一个简单,逻辑,优雅的机制。现在,要创建一个新的对象来创建一个新类,你只需要执行一个深层拷贝,复制所有的数据和原型数据结构。你可以获得继承和多态性,然后是:方法查找总是包括按名称询问一个方法实现的字典。

Now, why do that? primarily because it makes for a simple, logical, elegant mechanism at run time. now, to create a new object, or to create a new class, you simply have to perform a deep copy, copying all the data and the prototype data structure. You get inheritance and polymorphism more or less for free then: method lookup always consists of asking a dictionary for a method implementation by name.

最终在Javascript / ECMA脚本中的原因基本上是当我们在10年前开始的时候,我们正在处理的功能更少的电脑和更不复杂的浏览器。选择基于原型的方法意味着解释器可以非常简单,同时保留对象定向的理想属性。

The reason that ended up in Javascript/ECMA script is basically that when we were getting started with this 10 years ago, we were dealing with much less powerful computers and much less sophisticated browsers. Choosing the prototype-based method meant the interpreter could be very simple while preserving the desirable properties of object orientation.

这篇关于基于原型的基于类的继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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