Prototype-based programming

Prototype -Based Programming, also known as classless object orientation, is a form of object-oriented programming, which dispenses with the language element of the class. Objects are not created when compiling but at runtime by cloning existing objects. In this cloning, all properties (attributes and methods) of the prototype object are copied, but they can be overwritten, and the new object can be provided with new properties.

All existing objects can be prototypes of new objects.

Some languages ​​completely copy the objects cloning and there remains no connection between the clone and its prototype; but most of the prototype-based languages ​​use a special attribute, via which a connection is maintained by the prototype clone. This connection is cloned from the prototype objects inherit subsequent changes to the prototype.

The objects can be understood as an associative array; the key of this table are usually called slot, which usually not between attributes (data ) and methods ( operations ), a distinction of the object: Methods slots often only reference outside the object underlying program code.

Pros and Cons

Prototype -based languages ​​allow for greater flexibility in modeling because it does not force the programmer to bring his objects in a static, fixed at compile class structure. Objects remain at runtime structurally altered. However, this flexibility also includes the risk of a worse maintainability of the program.

Many optimizations that can already make at compile time, the compiler in class-based languages ​​, can not be realized due to the dynamic nature of prototype-based languages ​​with these.

Example

The following example in ECMAScript 5 first creates an object object1, which we will then use as a prototype. It has the attributes of A and B, which are assigned to the values ​​equal to 1 or 2.

Var object1 = {a: 1, b: 2 }; There are clones in ECMAScript 5 a predefined function Object.create (). The desired prototype is given as an argument. This clone gets in the example object2 the name.

Var object2 = Object.create ( object1 ); This new object can then be assigned to other attributes

Object2.c = 3; object2.d = 4; The cloned object now has four attributes:

  • A = 1 (inherited from prototype )
  • B = 2 (inherited from prototype )
  • C = 3 ( adds after cloning )
  • D = 4 ( adds after cloning )

List prototype -based programming

  • Self
  • ActionScript ( AS3 since increasingly class- oriented)

In principle can be in any programming language that supports pointers, Programming prototype- based. For Ruby and Perl prefabricated modules that support it exist.

662951
de