ITWissen.info - Tech know how online

Prototype

In the context of object-oriented programming, a prototype is an object that serves as a template for the generation of further objects. The template can be again a copy of an object or also an appropriate reference to this. In principle all already existing objects can be considered as prototypes for new objects. The inherited properties - these are attributes and methods - behave like the original properties of the prototype.

JavaScript is an example of a programming language that supports this object-based inheritance mechanism. One advantage of this mechanism is that prototypes and objects derived from them can be modified at program runtime. One speaks here also of prototype-based programming or likewise of the classless object orientation.

The prototype for objects

When objects are created on the basis of classes, their structure is determined by the structure of the class. Thus the characteristics of an object - i.e. its attributes and methods - are already fixed with their production, whereby these are not modifiable then any longer. The class is the underlying "tree pattern" of the object. An alternative way goes the prototype-based programming. Here a new object is created on the basis of a prototype, and the concrete structure of the object is specified only afterwards . Thus for example attributes and/or methods can be supplemented, in order to be able to affect so the behavior of the new object specifically. Also the prototype can be modified, in order to generate so a certain behavior of an object. Thereby in consequence naturally all objects produced on basis of these changed prototypes are affected by these modifications. This mechanism of the adjustment of prototypes at the run time of a program has however the obvious disadvantage of possible side effects or that of the worse maintainability of programs. The flexibility of prototype-based programming languages with this form of the modeling causes also that optimizations, as they are accomplished by compilers of class-based structures during the translation, are not possible in such a way. This can also have a negative effect on the performance of prototype-based applications.

JavaScript as a prototype-based programming language

A popular example of a prototype-based programming language is JavaScript, which does not support the concept of classes - and thus class-based inheritance. However, because of the flexibility of the language, JavaScript can be used to look like a programming language with classic inheritance mechanisms. For this purpose, classes are declared by functions and instances are created with the new operator, which is also known in JavaScript. The resulting objects then behave similarly to objects in, for example, Java, C++, etc.

In JavaScript, an object is initially declared without any specific properties. Only when the new object already exists, an object is assigned with the prototype construct, which represents the template (the prototype object). Thus all properties - attributes as well as methods - are completely mapped to the new object. Thus, the new object practically receives a reference to the prototype object. Again, any modifications made to the template will be visible in the objects created based on it. However, because there is only ever one prototype construct for a function, multiple inheritance is not possible.

Example of a prototype construct

Example of a prototype construct

First the constructor for the class is created - this is - as usual in Java and other programming languages - the name of the class. The instance attributes are created with this in the constructor. For the creation of methods, they are inserted into the prototype object of the class.

Constructor marked with new

Constructor marked with new

To create an object, the above class must be instantiated. To do this, its constructor must be called with new.

Informations:
Englisch: Prototype
Updated at: 29.10.2013
#Words: 611
Links: indium (In), object (O), template, copy (CPY), JavaScript
Translations: DE
Sharing:    

All rights reserved DATACOM Buchverlag GmbH © 2024