Języki oparte na klasach vs. oparte na prototypach

przez 5 współtwórców

UWAGA: Tłumaczenie tej strony nie zostało zakończone.
Może być ona niekompletna lub wymagać korekty.
Chcesz pomóc? | Dokończ tłumaczenie | Sprawdź ortografię | Więcej takich stron+.

Języki oparte na klasach vs. oparte na prototypach

Oparte na klasach języki obiektowe, takie jak Java i C++, powstały według koncepcji dwóch różnych encji: klas i instancji.

  • Klasa definiuje wszystkie własności (uznając metody i pola w Javie lub składowe w C++ również jako własności), które charakteryzują konkretny zbiór obiektów. Klasa jest bardziej abstrakcyjną rzeczą niż dowolna składowa zbioru obiektów, który jest przez nią definiowany. Na przykład, klasa Employee mogłaby reprezentować zbiór wszystkich pracowników.
  • An instance, on the other hand, is the instantiation of a class; that is, one of its members. For example, Victoria could be an instance of the Employee class, representing a particular individual as an employee. An instance has exactly the properties of its parent class (no more, no less).

A prototype-based language, such as JavaScript, does not make this distinction: it simply has objects. A prototype-based language has the notion of a prototypical object, an object used as a template from which to get the initial properties for a new object. Any object can specify its own properties, either when you create it or at run time. In addition, any object can be associated as the prototype for another object, allowing the second object to share the first object's properties.


Definiowanie klasy

In class-based languages, you define a class in a separate class definition. In that definition you can specify special methods, called constructors, to create instances of the class. A constructor method can specify initial values for the instance's properties and perform other processing appropriate at creation time. You use the new operator in association with the constructor method to create class instances.

JavaScript follows a similar model, but does not have a class definition separate from the constructor. Instead, you define a constructor function to create objects with a particular initial set of properties and values. Any JavaScript function can be used as a constructor. You use the new operator with a constructor function to create a new object.

Podklasy i dziedziczenie

In a class-based language, you create a hierarchy of classes through the class definitions. In a class definition, you can specify that the new class is a subclass of an already existing class. The subclass inherits all the properties of the superclass and additionally can add new properties or modify the inherited ones. For example, assume the Employee class includes only the name and dept properties, and Manager is a subclass of Employee that adds the reports property. In this case, an instance of the Manager class would have all three properties: name, dept, and reports.

JavaScript implements inheritance by allowing you to associate a prototypical object with any constructor function. So, you can create exactly the Employee- Manager example, but you use slightly different terminology. First you define the Employee constructor function, specifying the name and dept properties. Next, you define the Manager constructor function, specifying the reports property. Finally, you assign a new Employee object as the prototype for the Manager constructor function. Then, when you create a new Manager, it inherits the name and dept properties from the Employee object.

Dodawanie i usuwanie własności

In class-based languages, you typically create a class at compile time and then you instantiate instances of the class either at compile time or at run time. You cannot change the number or the type of properties of a class after you define the class. In JavaScript, however, at run time you can add or remove properties from any object. If you add a property to an object that is used as the prototype for a set of objects, the objects for which it is the prototype also get the new property.

Różnice

Następująca tabela daje krótkie podsumowanie niektórych z tych różnic. some of these differences. The rest of this chapter describes the details of using JavaScript constructors and prototypes to create an object hierarchy and compares this to how you would do it in Java.

Class-based (Java) Prototype-based (JavaScript)
Klasy i instancje sa odzielnymi encjami. Wszystkie obiekty są instancjami.
Definiuje klasy z klasą definicji; bieżace klasy z konstruktorem metod. Definiuje i tworzy ustawienia obiektów z konstruktorem funkcji.
Tworzy pojedyńczy obiekt z operatorem new. To samo.
Construct an object hierarchy by using class definitions to define subclasses of existing classes. Construct an object hierarchy by assigning an object as the prototype associated with a constructor function.
Inherit properties by following the class chain. Inherit properties by following the prototype chain.
Class definition specifies all properties of all instances of a class. Cannot add properties dynamically at run time. Constructor function or prototype specifies an initial set of properties. Can add or remove properties dynamically to individual objects or to the entire set of objects.


Autorzy i etykiety dokumentu

Contributors to this page: teoli, Mgjbot, Diablownik, Ptak82, gandalf
Ostatnia aktualizacja: teoli,