Prototype-Based Programming — Explained with Examples
Prototype-based programming is a style of object-oriented programming where objects inherit from other objects directly — there are no classes. Instead, you create a prototype object and then clone or extend it to create new objects. Inheritance is achieved through delegation: when a property or method is not found on an object, the runtime looks up the prototype chain.
JavaScript is the most widely used prototype-based language, using the [[Prototype]] internal slot (accessible via __proto__ or Object.getPrototypeOf()). In ES6+, JavaScript offers class syntax, but it is syntactic sugar over prototype-based inheritance. Other prototype languages include Self (the original), Lua (via metatables), and Io. Prototype-based programming can be more flexible than OOP class-based programming because objects can be modified at runtime.
Real-world analogy. Prototype-based programming is like a budding chef learning from a master chef. Instead of following a “Chef” blueprint (class), the apprentice shadows the master (prototype). If the apprentice doesn’t know how to julienne carrots, she watches the master — it’s delegated. If the apprentice learns a new technique, she can improve upon the master’s method without changing the master.
Example (JavaScript prototype chaining):
const animal = { speak() { return "?"; } };
const dog = Object.create(animal);
dog.speak = () => "Woof!";
const puppy = Object.create(dog);
puppy.speak(); // "Woof!" (from dog)
puppy.bark; // undefined → dog → animal → Object.prototype → null
Related terms: OOP, Functional Programming, Class-based OOP, Delegation, V8 Engine
Related tutorial: Prototype vs Class
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro