What does it mean that Javascript is a prototype based language?
Asked Answered
N

8

283

One of the major advantages with Javascript is said to be that it is a prototype based language.

But what does it mean that Javascript is prototype based, and why is that an advantage?

Nerva answered 9/10, 2008 at 7:22 Comment(1)
This answer explains everything you need to know about prototypal inheritance: https://mcmap.net/q/21076/-benefits-of-prototypal-inheritance-over-classicalLoosen
N
323

Prototypal inheritance is a form of object-oriented code reuse. Javascript is one of the only [mainstream] object-oriented languages to use prototypal inheritance. Almost all other object-oriented languages are classical.

In classical inheritance, the programmer writes a class, which defines an object. Multiple objects can be instantiated from the same class, so you have code in one place which describes several objects in your program. Classes can then be organized into a hierarchy, furthering code reuse. More general code is stored in a higher-level class, from which lower level classes inherit. This means that an object is sharing code with other objects of the same class, as well as with its parent classes.

In the prototypal inheritance form, objects inherit directly from other objects. All of the business about classes goes away. If you want an object, you just write an object. But code reuse is still a valuable thing, so objects are allowed to be linked together in a hierarchy. In javascript, every object has a secret link to the object which created it, forming a chain. When an object is asked for a property that it does not have, its parent object will be asked... continually up the chain until the property is found or until the root object is reached.

Each function in JavaScript (which are objects themselves) actually has a member called "prototype", which is responsible for providing values when an object is asked for them. Having this member allows the constructor mechanism (by which objects are constructed from functions) to work. Adding a property to the prototype of a function object will make it available to the constructed object, as well as to all of the objects which inherit from it.

Advantages

There may not be a hard and fast rule as to why prototypal inheritance is an advantageous form of code-reuse. Code reuse itself is advantageous, and prototypal inheritance is a sensible way of going about it. You might argue that prototypal inheritance is a fairly simple model of code reuse, and that code can be heavily reused in direct ways. But classical languages are certainly able to accomplish this as well.

Sidenote: @Andrew Hedges makes a good point, that there are actually many prototypal languages. It's worth noting that these others exist, but also worth noting that none of them are anything close to mainstream. NewtonScript seemed to have some traction for a while, but died with its platform. It's also possible to extend some modern languages in ways which add prototypal capabilities.

Neper answered 9/10, 2008 at 7:23 Comment(5)
Hey Kelly. While JavaScript is by far the most popular prototypal language, there are many others: en.wikipedia.org/wiki/Prototype-based_programming#LanguagesSpencerspencerian
Hey Andrew. Good point. I should have been more clear. I'll make a note of it.Neper
Please read this also developer.mozilla.org/en/JavaScript/Guide/…Together
+1 for a great answer. One minor comment: To me, classical inheritance seems more "direct" than prototypal. In fact, I really see the prototype object as mere linkage (to other objects), whereas in a compiled OOP I consider the base-class to be "directly inherited". As such, prototypal-objects are chained-together rather than inherited (inheritance is somewhat faked). Any thoughts?Disengagement
@PrisonerZERO: I'd argue that prototypal inheritance is more direct than classical. Instead of object B pointing to a class that inherits from the class that object A points at, it points directly at object A and says "i'm just like that object, except...". The big thing about prototypal inheritance, and the thing that seems hardest for most people to internalize, is that it doesn't distinguish instances from types. Every object is both a type and an instance. Distinctions between the two are artificial and deliberate, and are usually a symptom of being stuck in a class-oriented mindset.Tersanctus
C
60

A prototype-based language, does not make the distinction of classes vs objects: 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.

Cosine answered 9/10, 2008 at 7:26 Comment(1)
A very good explanation, but a little bit misleading with the comment about the "template for initial properties". If you change the prototype AFTER instantiating an object, that object still receives those functions.Cattier
S
41

Prototype-based programming is a style of object-oriented programming where classes are not present, and behavior reuse (or inheritance in class-based languages) is performed by cloning existing objects that serve as prototypes.

Scammon answered 9/10, 2008 at 7:27 Comment(1)
Do you still feel this way? Because if so then this is the first explanation to ever really just solidly "click" with me.Terebinthine
U
12

The advantage/disadvantage is that, we can create new kinds of objects at run time without need for defining classes (static code). Like most features it is upto the developer to turn it to a advantage/disadvantage.

Above is possible because objects are essentially functions in java script (closures too).

Unequaled answered 9/10, 2008 at 7:45 Comment(2)
Dynamic objects are a benefit of javascript, but they aren't really related to javascript being a prototypal or functional language. In many classical languages you can create dynamic objects at runtime. Closures are somewhat unrelated, as well.Neper
Classes are not necessarily a static code - take a look at Python, in which classes are objects themselves, and are constructed out of metaclasses which also are objects.Traumatize
C
9

After reading all the answers this is the conclusion

1) Inheritance in which objects are inherited directly from other objects

2) That does not use classes

3) Also called instance based programming or classless prototype oriented programming

4) Behaviour reuse is performed by cloning existing objects that serve as prototypes

5) Object used as template from the new object get initial properties

Countryandwestern answered 10/6, 2016 at 12:19 Comment(0)
E
7

Instead of declaring a class structure, you can create objects of the same type, and add to their definition any time you like using the object's prototype. It's more flexible than the normal way of doing things.

Estell answered 9/10, 2008 at 7:25 Comment(0)
I
6

If you just use objects at runtime instead of a class at compile to build new objects, this opens up the possibility of extending an object without knowing any details about it. Of course, it may become a disadvantage pretty quickly depending on usage. I make no assumptions about the language here, so it is applicable to languages other than javascript which are not as dynamic.

myobject.prototype=unkownobject;
myobject.newproperty=1;

You may get the object from just about anywhere; your own code, from the network, from the database, from external linkage and so on.

Note that, a language don't have to implement prototype inheritance like javascript. In javascript, a prototype object is merely shared, so is its properties, among the inheritors. The alternative is copying over all the properties of the prototype to the new object. Each approach has its strengths in different situations. I like the second more but it isn't what javascript does.

Intertexture answered 9/10, 2008 at 8:14 Comment(0)
D
0

Memory conservation is one of the benefits of prototypal inheritance in JS. In a language like Java, objects generate their own copies of the superclass' instance variables and methods, while in JS, the "super"-object offers get-access to its variables and methods to each "sub"-object that inherits from it without the need to recreate them.

Dina answered 14/8, 2021 at 11:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.