What is the prototype in JavaScript?
Answer
Every JavaScript object has an internal link to another object called its prototype. When you access a property on an object and it is not found directly on the object, JavaScript looks up the prototype chain — checking the prototype, then the prototype's prototype, and so on until it reaches null. This is JavaScript's inheritance mechanism. Object.getPrototypeOf(obj) returns the prototype. For objects created with object literals, the prototype is Object.prototype. For arrays, the prototype chain is: array → Array.prototype → Object.prototype → null. When you define a method on Array.prototype, all arrays inherit it. Constructor functions: function Person(name) { this.name = name; } Person.prototype.greet = function() { return "Hi, " + this.name; }. ES6 classes are syntactic sugar over prototype-based inheritance.
Previous
What are JavaScript modules (import/export)?
Next
What is the difference between call(), apply(), and bind()?