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.
Code
// 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! startedWhat 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
PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
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`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.