Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
592 views
in Technique[技术] by (71.8m points)

javascript - 基于原型与基于类的继承(prototype based vs. class based inheritance)

In JavaScript, every object is at the same time an instance and a class.(在JavaScript中,每个对象同时是一个实例和一个类。)

To do inheritance, you can use any object instance as a prototype.(要进行继承,可以将任何对象实例用作原型。) In Python, C++, etc.. there are classes, and instances, as separate concepts.(在Python,C ++等中。有类和实例作为单独的概念。) 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.(为了进行继承,您必须使用基类创建一个新类,然后该新类可用于生成派生实例。) Why did JavaScript go in this direction (prototype-based object orientation)?(为什么JavaScript朝这个方向发展(基于原型的面向对象)?) what are the advantages (and disadvantages) of prototype-based OO with respect to traditional, class-based OO?(与传统的基于类的OO相比,基于原型的OO有哪些优点和缺点?)   ask by Stefano Borini translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:(所有面向对象的语言都必??须能够处理以下几个概念:) encapsulation of data along with associated operations on the data, variously known as data members and member functions, or as data and methods, among other things.(数据封装以及对数据的关联操作(除其他外,各种操作被称为数据成员和成员函数,或者称为数据和方法)。) inheritance, the ability to say that these objects are just like that other set of objects EXCEPT for these changes(继承性,可以说这些对象就像其他对象一样,但这些更改除外) polymorphism ("many shapes") in which an object decides for itself what methods are to be run, so that you can depend on the language to route your requests correctly.(多态(“许多形状”),在这种多态中,对象可以自行确定要运行的方法,因此您可以依靠语言来正确地路由请求。) Now, as far as comparison:(现在,就比较而言:) 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.(这个想法最初是从Simula开始的,在Simula中,每个类都使用基于类的方法来代表一组对象,这些对象共享相同的状态空间(读取“可能的值”)和相同的操作,从而形成一个等效类。) 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.(如果回顾一下Smalltalk,由于可以打开一个类并添加方法,因此这实际上与Javascript相同。) 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.(后来的OO语言希望能够使用静态类型检查,因此我们有了在编译时设置固定类的概念。) 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.(然后,您或多或少可以免费获得继承和多态性:方法查找始终包括按名称向字典请求方法实现。) 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.(最终以Javascript / ECMA脚本结尾的原因基本上是,当我们在10年前开始使用它时,我们所使用的功能却差强人意的计算机和复杂得多的浏览器。) Choosing the prototype-based method meant the interpreter could be very simple while preserving the desirable properties of object orientation.(选择基于原型的方法意味着解释器可能非常简单,同时保留了面向对象的理想特性。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...