How is the prototype system organized in JavaScript?
sobes.tech AI
Answer from AI
In JavaScript, the prototype system is a inheritance mechanism where objects can inherit properties and methods from other objects.
Each object has an internal property [[Prototype]] (accessible via __proto__ or Object.getPrototypeOf), which points to another object — the prototype. If a property is not found directly on the object, the search continues in the prototype, then in the prototype's prototype, and so on, until null is reached.
This allows creating prototype chains and implementing inheritance without classes (although modern JS versions have class syntax, which under the hood uses prototypes).
Example:
const animal = {
speak() {
console.log('Animal speaks');
}
};
const dog = Object.create(animal);
dog.bark = function() {
console.log('Woof!');
};
dog.bark(); // Woof!
dog.speak(); // Animal speaks (inherited from animal)
Thus, prototypes allow objects to share behavior and properties, which is the foundation of object-oriented programming in JavaScript.