Facebook Pixel

Prototype Interview Questions in JavaScript

Common prototype and prototypal inheritance interview questions.

Prototype Interview Questions

Q1: What is the prototype chain?

The chain of objects JS traverses to find a property: obj -> proto -> proto.proto -> null.

Q2: What is the difference between proto and prototype?

__proto__ is on every object (its actual prototype). prototype is on functions (the object that instances will inherit from).

Q3: How do you implement inheritance with constructor functions?

Student.prototype = Object.create(Person.prototype); Student.prototype.constructor = Student;

Q4: Are ES6 classes different from prototypes?

No. Classes are syntactic sugar over the prototype system.

Q5: What does Object.create(null) do?

Creates an object with no prototype. Useful for hash maps (no inherited properties like toString).

Q6: How do you check if a property is inherited?

!obj.hasOwnProperty(key) && key in obj - the property exists but is not own.

The Takeaway

Interview questions: prototype chain (lookup mechanism), proto vs prototype, inheritance with constructors (Object.create + constructor), ES6 classes (syntactic sugar), Object.create(null) (no prototype), and checking inherited properties.

__proto__ is on every object and points to its actual prototype. prototype is on functions and is the object that instances created with new will inherit from. obj.__proto__ === Constructor.prototype for objects created with new Constructor().

Student.prototype = Object.create(Person.prototype); Student.prototype.constructor = Student;. In the constructor: Person.call(this, name). This sets up the prototype chain manually.

Creates an object with no prototype (no __proto__). It has no inherited properties (no toString, no hasOwnProperty). Useful for hash maps or dictionaries where you do not want any inherited properties.

No. ES6 classes are syntactic sugar over the prototype system. class Person { greet() {} } puts greet on Person.prototype. extends sets up the prototype chain. It is the same mechanism with cleaner syntax.

Use hasOwnProperty: obj.hasOwnProperty('key') is true for own, false for inherited. To check if a property exists but is inherited: !obj.hasOwnProperty(key) && key in obj.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.