Autocoder

JavaScript Prototypes and Inheritance Interview Questions

Last updated 2026-11-19

JavaScript uses prototype-based inheritance: objects delegate to other objects through an internal link rather than copying members from classes. Interviews test whether you understand that link, how new connects objects, and what the class syntax actually does.

The answers below explain the chain with runnable examples, from the difference between __proto__ and prototype to hand-written inheritance with Object.create, the behaviour of property shadowing and the security issue of prototype pollution.

Level1 to 5 years
FocusPrototype chain, new, classes, inheritance patterns
RuntimeBrowser and Node.js

Two names, one idea

Every object has an internal prototype link, exposed as __proto__ or read with Object.getPrototypeOf. Every ordinary function also has a prototype property, which is just an object that becomes the link for instances created with new. Mixing these up is the most common error.

Related topics

Method calls and context are in the this keyword, and the modern syntax in ES6 features. Preparation guidance is in how to crack a JavaScript interview.

JavaScript Prototypes and Inheritance Interview Questions: questions with worked answers

  1. 1

    What is the prototype chain?

    easy

    When you read a property, the engine looks at the object itself, then at its prototype, then at that object's prototype, and so on until it finds the property or reaches null, which gives undefined. That ordered list of objects is the prototype chain. Methods like toString are found on Object.prototype at the end of the chain. Writes always go on the object itself, never on the prototype.

  2. 2

    What is the difference between __proto__ and prototype?

    medium

    __proto__ is the accessor for an object's internal prototype link. prototype is a property of constructor functions and classes that holds the object that will be the link for instances. So instance.__proto__ === Constructor.prototype. Prefer Object.getPrototypeOf and Object.setPrototypeOf over __proto__ in modern code.

    javascript
    function Dog(n) { this.n = n; }
    Dog.prototype.bark = function () { return this.n + " woof"; };
    const d = new Dog("Rex");
    console.log(d.__proto__ === Dog.prototype);                // true
    console.log(Object.getPrototypeOf(Dog.prototype) === Object.prototype);  // true
    console.log(d.bark(), d.hasOwnProperty("bark"));          // Rex woof false
  3. 3

    What exactly does the new operator do?

    medium

    It performs four steps: create a new empty object, set its prototype to the constructor's prototype property, call the constructor with this bound to that object, and return the object unless the constructor returns another object. A hand-written version makes this clear.

    javascript
    function myNew(Ctor, ...args) {
      const obj = Object.create(Ctor.prototype);
      const res = Ctor.apply(obj, args);
      return res !== null && (typeof res === "object" || typeof res === "function") ? res : obj;
    }
    function P(n) { this.n = n; }
    const p = myNew(P, "x");
    console.log(p.n, p instanceof P);   // x true
  4. 4

    How does Object.create work?

    medium

    Object.create(proto) returns a new empty object whose prototype is proto, with no constructor call. Passing null creates an object with no prototype at all, useful as a dictionary free of inherited keys such as toString. A second argument takes property descriptors.

    javascript
    const base = { hi() { return "hi " + this.n; } };
    const o = Object.create(base);
    o.n = "x";
    console.log(o.hi(), Object.getPrototypeOf(o) === base);   // hi x true
    const dict = Object.create(null);
    console.log("toString" in dict);                           // false
  5. 5

    Implement inheritance between constructor functions.

    hard

    Link the child's prototype to a new object that inherits from the parent's prototype, restore the constructor property, and call the parent constructor inside the child constructor to initialise own properties. Assigning Child.prototype = Parent.prototype would be wrong, since changes to the child would alter the parent.

    javascript
    function Animal(name) { this.name = name; }
    Animal.prototype.speak = function () { return this.name + " makes a sound"; };
    
    function Dog(name) { Animal.call(this, name); }
    Dog.prototype = Object.create(Animal.prototype);
    Dog.prototype.constructor = Dog;
    Dog.prototype.speak = function () {
      return Animal.prototype.speak.call(this) + " (woof)";
    };
    
    const d = new Dog("Rex");
    console.log(d.speak());               // Rex makes a sound (woof)
    console.log(d instanceof Animal);     // true
  6. 6

    Are ES6 classes real classes?

    medium

    No, they are syntactic sugar over prototypes and constructor functions, with some differences. A class is a function, is not hoisted like a function declaration, runs in strict mode, and throws if called without new. Methods are non-enumerable. extends sets up both the instance chain and the chain between the constructors so static members are inherited.

    javascript
    class A { hi() { return "A"; } static s() { return "static"; } }
    class B extends A {}
    console.log(typeof A, B.s());                                   // function static
    console.log(Object.getPrototypeOf(B) === A);                    // true
    console.log(Object.getPrototypeOf(B.prototype) === A.prototype); // true
    try { A(); } catch (e) { console.log(e.message); }
    // Class constructor A cannot be invoked without 'new'
  7. 7

    How does super work in classes?

    medium

    In a constructor of a derived class, super(...) calls the parent constructor and must run before this is used, otherwise a ReferenceError is thrown. In methods, super.method() calls the parent's version through the prototype chain, with this unchanged. Static methods use super to reach parent static members.

    javascript
    class Shape {
      constructor(n) { this.n = n; }
      describe() { return "shape " + this.n; }
    }
    class Circle extends Shape {
      constructor() { super("circle"); }
      describe() { return super.describe() + "!"; }
    }
    console.log(new Circle().describe());   // shape circle!
  8. 8

    Predict the output: property shadowing and shared prototype state.

    hard

    Reading finds the prototype's property, but assigning creates an own property that shadows it. A mutable object on the prototype is shared by all instances, so pushing to it affects everyone, while reassigning on one instance affects only that instance.

    javascript
    function C() {}
    C.prototype.list = [];
    C.prototype.n = 1;
    const a = new C(), b = new C();
    a.list.push("x");     // mutates the shared array
    a.n = 5;              // creates own property on a
    console.log(b.list, b.n, a.n);   // [ 'x' ] 1 5
  9. 9

    What do instanceof, isPrototypeOf and hasOwnProperty check?

    medium

    x instanceof F tests whether F.prototype appears anywhere on x's prototype chain. A.isPrototypeOf(x) tests whether A is on the chain. Object.hasOwn(x, key), the modern form of hasOwnProperty, tests only own properties and ignores the chain. The in operator checks the whole chain.

    javascript
    const base = { a: 1 };
    const o = Object.create(base);
    o.b = 2;
    console.log("a" in o, Object.hasOwn(o, "a"), Object.hasOwn(o, "b"));  // true false true
    console.log(base.isPrototypeOf(o));                                    // true
  10. 10

    Why define methods on the prototype instead of in the constructor?

    medium

    Methods defined on the prototype exist once and are shared by all instances, saving memory. Methods created inside the constructor are new function objects for every instance. The trade-off is that constructor-defined methods can close over private variables, and arrow class fields also create a copy per instance.

    javascript
    class A { m() {} }
    const a1 = new A(), a2 = new A();
    console.log(a1.m === a2.m);   // true: shared
    function B() { this.m = function () {}; }
    console.log(new B().m === new B().m);   // false: one per instance
  11. 11

    What is prototype pollution?

    hard

    Prototype pollution happens when attacker-controlled keys such as __proto__ or constructor.prototype are written during an unsafe merge or deep assignment, so properties are added to Object.prototype and appear on every object in the process. It can change application logic or lead to remote code execution in some setups. Defend by rejecting those keys, using Map or Object.create(null) for dictionaries, freezing prototypes, and keeping merge libraries up to date.

    javascript
    const o = {};
    o.__proto__.polluted = true;        // writes to Object.prototype
    console.log({}.polluted);           // true
    delete Object.prototype.polluted;
  12. 12

    How do you check or change an object's prototype, and why avoid setPrototypeOf?

    medium

    Object.getPrototypeOf reads the link and Object.setPrototypeOf changes it. Changing the prototype of an existing object defeats engine optimisations and can slow all code that touches the object, so create the object with the right prototype using Object.create or class syntax instead.

  13. 13

    Predict the output: what is the constructor property after replacing the prototype?

    hard

    A default prototype object has a constructor property pointing to the function. Replacing the whole prototype with an object literal removes it, so the property lookup falls up to Object.prototype and returns Object. Restore it when overwriting a prototype.

    javascript
    function F() {}
    console.log(F.prototype.constructor === F);    // true
    F.prototype = { x: 1 };
    const f = new F();
    console.log(f.constructor === F, f.constructor === Object);   // false true
  14. 14

    How do mixins work in JavaScript?

    medium

    JavaScript classes have single inheritance, so shared behaviour is composed with mixins. A mixin is a function that takes a base class and returns a subclass that adds methods, or an object whose methods are copied onto a prototype with Object.assign. Prefer composition of small functions where possible, since deep mixin stacks are hard to follow.

    javascript
    const Flies = (Base) => class extends Base { fly() { return this.n + " flies"; } };
    class Bird { constructor(n) { this.n = n; } }
    class Eagle extends Flies(Bird) {}
    console.log(new Eagle("eagle").fly());   // eagle flies

Frequently asked questions

Is JavaScript object-oriented?

Yes, but prototype-based. Objects inherit directly from other objects. The class keyword gives a familiar syntax over the same mechanism.

What is at the end of the prototype chain?

Object.prototype, whose own prototype is null. Objects created with Object.create(null) have no chain at all.

What is the difference between own and inherited properties?

Own properties are stored on the object itself, and inherited ones are found through the chain. Object.keys and Object.hasOwn deal only with own properties.

Should I use classes or prototypes directly?

Use classes for clarity in almost all code. Understand prototypes to debug behaviour, read older code and answer interview questions.

Related guides

More in Programming Language Interview Questions by Language

See all in this section