JavaScript Prototypes & Inheritance Interview Questions and Answers
Understand prototypes, the prototype chain, and how JavaScript inheritance works under class syntax.
Frequently Asked Questions
Important questions are highlighted in the sections below; select one to jump to its answer.
- What is the prototype chain?
- How can you check whether a property belongs directly to an object?
- What does a constructor function prototype property represent?
- Add a shared greeting method through a prototype
- What is the difference between prototype, __proto__, and the prototype chain?
- Prototype-based inheritance in JavaScript? How does it differ from classical inheritance in languages like Java or C++?
- Create an object that inherits a method with Object.create.
Theoretical JavaScript Interview Questions
In JavaScript,
prototype-based
inheritance means objects inherit through a prototype
chain. When a property is not found on an object,
JavaScript looks at its prototype and continues upward
until it finds the property or reaches null. The
class syntax creates constructor functions
and prototype methods while adding class-style rules,
so it is more precise to call it an abstraction over
prototypes than simply "syntactic sugar." Java and C++
organize inheritance around classes, whereas JavaScript
uses objects and prototypes at runtime.
prototype is a property on constructor functions and classes that is used when new objects are created with new. __proto__ is the prototype of a specific object instance, though it is considered a legacy way to access it. The prototype chain is the full lookup path JavaScript follows from one prototype to the next when searching for a property or method.
It is the sequence of objects JavaScript searches when a property is not found directly on the current object, ending at null.
Use Object.hasOwn(object, key) to test for an own property; the in operator also finds properties inherited through the prototype chain.
For a constructor function, its prototype property is the object assigned as the prototype of instances created with new.
It checks whether a constructor prototype appears in an object prototype chain, not whether an object was created by a particular source-level class declaration.
It creates a new object whose internal prototype is proto, or null if null is supplied, without invoking a constructor function.
A derived class prototype chain links to its base class, and its instances link to the base prototype. The derived constructor must call super() before using this.
The subclass own prototype method is found first during lookup, overriding the inherited method; super.method() can call the parent implementation where appropriate.
Yes. The constructor function objects themselves are linked through the class inheritance chain, so a derived class can access inherited static members unless it shadows them.
Validate and allowlist keys from untrusted input, use own-property checks, and use Map or null-prototype dictionaries for arbitrary keys rather than unsafe recursive merges.
JavaScript Practical and Coding Interview Questions
Problem: Create a prototype with a describe method, derive a user object with Object.create, and log the inherited method result.
const personPrototype = {
describe() {
return this.name + ' is ' + this.role;
},
};
const user = Object.create(personPrototype);
user.name = 'Mina';
user.role = 'an engineer';
console.log(user.describe());Problem: Create a Shape class and a Rectangle subclass that overrides area. Instantiate the rectangle and log its area.
class Shape {
area() {
return 0;
}
}
class Rectangle extends Shape {
constructor(width, height) {
super();
this.width = width;
this.height = height;
}
area() {
return this.width * this.height;
}
}
console.log(new Rectangle(4, 3).area());Problem: Create a Person constructor that stores a name, add greet() once to Person.prototype, create two people, and log both greetings.
function Person(name) {
this.name = name;
}
Person.prototype.greet = function () {
return `Hello, ${this.name}!`;
};
const first = new Person('Mina');
const second = new Person('Omar');
console.log(first.greet());
console.log(second.greet());