Skip to the page

Objects, prototypes and classes · core

The prototype chain

Every object has a hidden link, [[Prototype]], to another object; when a property is not found on the object, the engine follows this chain until it finds it or reaches null — this is how methods are shared and how inheritance works.

The notes

What you must know

  • Object.getPrototypeOf(obj) is the standard way to read the link. obj.proto is a legacy accessor. Fn.prototype is the object that becomes the [[Prototype]] of instances created with new Fn().
  • Reads walk up the chain. Writes create or change an own property (shadowing), unless a setter or a non-writable property exists up the chain.
  • Chains: [] -> Array.prototype -> Object.prototype -> null. A function -> Function.prototype -> Object.prototype -> null.
  • Object.create(proto) makes an object with a chosen prototype; Object.create(null) makes one with none.
  • instanceof checks whether Ctor.prototype appears anywhere in the object's chain. It fails across iframes/realms, which is why Array.isArray exists.
  • Methods on a prototype are shared by all instances (one copy in memory); instance fields are per object.
  • Changing a prototype after creation (Object.setPrototypeOf) slows engine optimisations; set it when you create the object.

The red pen

Common traps: mistake → fix

  • Mistake: prototype and proto are the same

    Fix: prototype is a property of functions; [[Prototype]] (proto) is the link every object has.

  • Mistake: Classes copy methods into each instance

    Fix: methods live on the prototype.

  • Mistake: obj.x = 1 changes the prototype's x

    Fix: it creates an own property that shadows it.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is the prototype chain?
  2. Explain prototype vs proto vs Object.getPrototypeOf.
  3. Draw the chain for const d = new Dog() where Dog extends Animal.
  4. Implement myInstanceOf(obj, Ctor).
  5. Set up inheritance without class (Object.create plus calling the parent constructor). Why is setPrototypeOf on a live object discouraged?

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview