Prototype-based programming is a style of object-oriented programming in which objects are created by cloning or linking to existing objects, called prototypes, rather than by instantiating classes. Behavior is shared through delegation: when an object lacks a requested property, the runtime forwards the lookup to its prototype, and that mechanism is often called prototypal inheritance.
The approach originated in the Self language in the late 1980s and reached a mass audience through JavaScript, whose object model is prototype-based even when code uses the class keyword. Lua metatables, Io and the NewtonScript language follow the same idea. Understanding prototypes explains why JavaScript objects can gain methods at run time, why this behaves as it does, and where performance costs hide.
How delegation through the prototype chain works
Every object in a prototype-based system holds its own properties plus a hidden reference to a parent object. In JavaScript this internal link is [[Prototype]], readable with Object.getPrototypeOf(). When code reads obj.greet, the engine first searches the object itself, then its prototype, then the prototype of that prototype, until it finds the property or reaches null.
This sequence of linked objects is the prototype chain. Methods found higher in the chain run with this bound to the original receiving object, not to the prototype that stores the function. That rule is what lets thousands of objects share one copy of a method while each method call still operates on the correct instance data.
Writes behave differently from reads. Assigning obj.x = 5 normally creates or updates an own property on the object, shadowing any x further up the chain, so the prototype stays untouched. The table below summarizes the core operations and what each one does.
| Operation | JavaScript form | Effect on the chain |
|---|---|---|
| Create with a prototype | Object.create(proto) | New empty object linked to proto |
| Read a property | obj.name | Walks the chain until found or null |
| Write a property | obj.name = v | Creates an own property that shadows |
| Check own property | Object.hasOwn(obj, k) | Ignores inherited properties |
| Change the link | Object.setPrototypeOf(obj, p) | Rewires delegation, slow in engines |
Cloning, concatenation and delegation models
Prototype-based languages differ in how a new object obtains behavior from its prototype. The two classic strategies are delegation, where the new object keeps a live link, and concatenation, where it receives a full copy of the properties at creation time. Self combined both ideas with explicit parent slots that an object could have several of.
The choice has practical consequences. With live delegation, adding a method to a prototype immediately affects every object linked to it. With concatenative cloning, later changes to the original never reach existing copies, which isolates objects but multiplies memory use.
| Strategy | How behavior is obtained | Later prototype changes | Example |
|---|---|---|---|
| Delegation | Live link to the parent object | Visible to all linked objects | JavaScript, Self, Lua |
| Concatenation | Copy of all slots at creation | Not visible to existing copies | Kevo, Object.assign patterns |
| Multiple parents | Several parent slots searched in order | Visible through each parent | Self, Io |
Prototypes compared with class-based inheritance
The central difference is that class-based languages separate templates from instances, while prototype-based languages treat every object as a potential template. A class describes the shape of future objects and usually cannot change after compilation; a prototype is an ordinary object that can be inspected, extended and replaced while the program runs.
Modern JavaScript blurs the line. The class syntax introduced in ECMAScript 2015 is syntactic sugar over prototypes: a class declaration creates a constructor function whose prototype property holds the methods. Private fields and static blocks add class-like features, yet method lookup still follows the prototype chain.
| Aspect | Prototype-based | Class-based |
|---|---|---|
| Source of new objects | Cloning or linking an existing object | Instantiating a class definition |
| Inheritance unit | Individual objects | Classes and subclasses |
| Run-time flexibility | Methods added or removed freely | Structure fixed after declaration |
| Type checking | Usually dynamic, structural | Often static, nominal |
| Typical languages | JavaScript, Self, Lua, Io | Java, C#, C++, Smalltalk |
Related concepts sit close by. A mixin copies a bundle of methods into an object or prototype, which is concatenation in practice, while traits add rules for resolving name conflicts. Composition over inheritance is easy to follow in prototype systems because any object can serve as a delegate.
Performance and engine behavior
A naive implementation would search a dictionary at every level of the chain, which is slow. Production engines avoid this with hidden classes, also called shapes or maps, an idea pioneered by the Self team. Objects created the same way share a hidden class, and inline caches at each call site remember where a property was found last time.
The optimization works only while object shapes stay stable. Calling Object.setPrototypeOf on a live object, deleting properties, or adding them in varying order invalidates inline caches and can force the engine back to slow dictionary mode. Deep chains of more than a few levels also add lookup cost on cache misses.
- Assign all properties in the constructor, in the same order, to keep shapes monomorphic.
- Set the prototype at creation with
Object.createinstead of rewiring it afterward. - Keep chains shallow; two or three levels cover most designs.
Common mistakes and failure modes
Most defects in prototype-based code come from confusing own and inherited state. Because reads fall through to the prototype while writes do not, a mutable array or object stored on a prototype is silently shared by every instance, which is a classic source of cross-instance bugs.
| Symptom | Cause | Fix |
|---|---|---|
| Instances share one list | Mutable value on the prototype | Initialize the value in the constructor |
| Method loses its object | Detached `this` binding | Use bind, arrow functions or call on the object |
| Unexpected keys in loops | for...in includes inherited keys | Use Object.keys or Object.hasOwn |
| Security vulnerability | Prototype pollution via __proto__ keys | Validate input keys, use Object.create(null) |
| Library conflicts | Extending built-in prototypes | Never modify Array.prototype in shared code |
Prototype pollution deserves particular attention. When untrusted JSON is merged recursively into an object, a key named __proto__ can inject properties into Object.prototype, changing the behavior of every object in the process. Safe merge utilities reject such keys, and freezing the base prototype in sensitive services blocks the attack entirely.
A plain-language summary
In prototype-based programming there are no blueprints, only examples. Instead of drawing a plan for a chair and building chairs from the plan, a carpenter takes an existing chair and says: make a new one like this, but painted blue.
If someone asks the blue chair a question it cannot answer, it simply asks the original chair. That way, many objects can share the same skills without each one storing a copy. The key takeaway is that objects learn from other objects directly, which makes programs flexible but also means a change to the original can affect everything linked to it.
An example from practice
A front-end team maintains a dashboard that renders about 20,000 chart points. Each point originally received its own copies of draw, highlight and tooltip functions inside a factory, so memory profiling showed tens of thousands of identical function objects and noticeable garbage-collection pauses during scrolling.
The team moved the three methods onto a single prototype object and created each point with Object.create(pointProto), assigning only coordinates and color as own properties in a fixed order. The point objects now share one set of methods through delegation, and the engine sees a single stable shape.
Heap snapshots afterward showed the function count for points drop to three, memory used by chart objects fell by roughly a third, and frame times during scrolling became consistent. A code review also added a guard so that configuration merges reject __proto__ keys, closing a prototype pollution risk discovered during the refactor.