Full answer
In JavaScript, objects inherit directly from other objects. Each object has an internal slot called [[Prototype]], which can be read with Object.getPrototypeOf(obj). When code reads obj.speak, the engine checks obj itself first. If it is missing, it checks the prototype, then the prototype's prototype, and so on until it finds the property or reaches null and returns undefined. This is the prototype chain. Writing a property always creates it on the object itself (unless a setter is found), so assignment does not change the prototype.
There are three ways to set up a link:
Object.create(proto)makes a new object whose prototype isproto.- A constructor function called with
newcreates an object whose prototype is the function'sprototypeproperty. Methods are added toConstructor.prototype. - An ES6
classdoes the same with cleaner syntax.extendslinks the prototypes of the two classes and the constructor functions, andsupercalls up the chain. A class is still a function, and its methods are onClass.prototype.
Putting methods on the prototype means all instances share one copy, which saves memory compared with creating a function in every constructor call. instanceof walks the prototype chain, and Object.hasOwn tests only the object itself.
Classes add real differences: they are not hoisted for use, always run in strict mode, require new, and support private #fields. Modifying Object.prototype or built-in prototypes is considered unsafe.
const animal = {
describe() {
return `${this.name} makes ${this.sound()}`;
},
sound() {
return 'a sound';
},
};
const dog = Object.create(animal);
dog.name = 'Bruno';
dog.sound = () => 'a bark';
console.log(dog.describe());
console.log(Object.getPrototypeOf(dog) === animal, Object.hasOwn(dog, 'describe'));
function Person(name) {
this.name = name;
}
Person.prototype.greet = function () {
return `Hello, I am ${this.name}`;
};
const p1 = new Person('Anu');
const p2 = new Person('Vik');
console.log(p1.greet(), p1.greet === p2.greet);
class Employee extends Person {
#salary;
constructor(name, salary) {
super(name);
this.#salary = salary;
}
greet() {
return `${super.greet()}, I work here`;
}
get band() {
return this.#salary > 50000 ? 'senior' : 'junior';
}
}
const e = new Employee('Ravi', 60000);
console.log(e.greet(), e.band);
console.log(e instanceof Employee, e instanceof Person, e instanceof Object);
console.log(typeof Employee, Object.getPrototypeOf(Employee) === Person);
console.log(Object.getPrototypeOf(Object.getPrototypeOf(Object.getPrototypeOf(e))) === Object.prototype);
p1.greet = () => 'shadowed';
console.log(p1.greet(), p2.greet());
try {
Employee();
} catch (error) {
console.log(error.name);
}Run with Node.js v24.22.1 as js-25.js; two runs gave identical output. Output: Bruno makes a bark true false Hello, I am Anu true Hello, I am Ravi, I work here senior true true true function true true shadowed Hello, I am Vik TypeError
Key points
- Property lookup follows the [[Prototype]] chain until null.
- Assignment creates an own property; it does not change the prototype.
- Object.create, constructor functions with new, and classes all build prototype links.
- Methods on the prototype are shared by all instances.
- Classes are syntax over prototypes, with extra rules such as strict mode and private fields.
Common mistakes
- Saying classes replaced prototypes. They are built on them.
- Defining methods inside a constructor and creating one copy per instance.
- Assigning to an inherited property and expecting the prototype to change.
- Replacing Constructor.prototype with a new object and forgetting to restore the constructor property.
Follow-ups
- What is the difference between __proto__ and prototype?
- How does super work in an object literal method or in a static method?
- What happens to the chain when you use Object.create(null)?
Tips
- Draw the chain: instance, then Class.prototype, then Object.prototype, then null. A sketch makes the answer concrete.
- Show that methods are shared with the p1.greet === p2.greet check.
- Mention private fields as the main thing classes give that plain prototypes do not.