Prototype-based programming is a style of object-oriented programming in which inheritance is implemented via process of cloning existing objects that serve as prototypes. Read more about prototype-based OOP here. In prototype-based languages objects inherit directly from other objects, in more classical class-based OOP languages inheritance is based around "parent-child" relationship between classes.
Difference from class-based inheritance
In class-based OOP language inheritance and instantiation feel like two different and separate things. When you are defining a subclass, you are creating a new class that inherits members and behavior from its base class and then you extend it. After, if you need to manipulate state and data of your newly created subclass, you need to instantiate it. Then, and only then, you can treat it as an object.
In case of prototypal OOP, the acts of "inheritance" and "instantiation" feel like the same process. There is no notion of a "class", so you just skip process of defining a class. There are just objects. You instantiate them at the time of creation.
For example, in terms of class-based OOP,
Apple are base and child classes respectively. Usually
Fruit is an abstraction, which describes general "fruit" and even can't be instantiated.
Apple is a concrete class, which can be instantiated, it takes all properties of
Fruit and extends them with properties special to "apple". In case of prototype-based OOP
Fruit is not an abstraction, it is concrete object as well as
Apple, so you can put it in the basket (array), perform "eat" operation on it or send it to a friend (to function as an argument). Sounds not realistic, since you can't do this to fruit in real life, because there is no abstract "Fruit", only concrete ones. But what is not in line with real life, might be more suitable for programming needs.
Image source: slideshare.net
- Prototype-based programming is a style of OOP when inheritance is implemented via process of cloning existing objects.
- In class-based OOP inheritance is built around "parent-child" relationship between classes.
- There are advantages and disadvantages between this two approaches, so consider what suits your needs carefully.