Prototype Chains Explain instanceof

instanceof answers true for every ancestor on the prototype chain, Object.getPrototypeOf walks it hop by hop, and hasOwn ignores inherited members entirely.

JavascriptData Integrity

Code

javascriptsnippet
26 lines
// Each class links to its parent through the prototype chain.
class Transport {
  start() { return 'started'; }
}
class Bike extends Transport {
  ring() { return 'ring!'; }
}
const bike = new Bike();

// instanceof walks the chain: every ancestor matches.
console.log('Bike     :', bike instanceof Bike);
console.log('Transport:', bike instanceof Transport);
console.log('Object   :', bike instanceof Object);

// getPrototypeOf reveals the same links one hop at a time.
let hop = Object.getPrototypeOf(bike);
while (hop && hop.constructor.name !== 'Object') {
  console.log('chain hop:', hop.constructor.name);
  hop = Object.getPrototypeOf(hop);
}

// hasOwn stays on the instance, ignoring inherited methods.
console.log('own props:', Object.hasOwn(bike, 'ring'), Object.hasOwn(bike, 'start'));

// Inherited methods resolve through the chain they belong to.
console.log('methods  :', bike.ring(), bike.start());

Output

Captured from a sandboxed run of this snippet in the build pipeline.

Bike     : true
Transport: true
Object   : true
chain hop: Bike
chain hop: Transport
own props: false false
methods  : ring! started

What the code does

The working surface is the classes Transport and Bike — everything else is wiring.

Iteration happens inside while (hop && hop.constructor.name !== 'Object') — everything outside it runs once.

Top-level bindings bike, hop hold the demo state — nothing sits inside a function scope.

Using it

Output lines are prefixed (Transport:, chain hop:, own props:), making each step’s effect easy to spot.

Key statements

start() { return 'started'; }
ring() { return 'ring!'; }

Interactive Tools for This Topic

FAQ

Is the dependency story complicated?
Not at all: zero dependencies, so there is nothing to version or audit. Its state lives in module-level bindings: `bike`, `hop`.