Object.create(proto) creates a new object with a specified prototype — the purest form of prototypal inheritance. instanceof checks if a constructor's prototype appears anywhere in an object's prototype chain. This article implements both from scratch.
Prerequisites: JS Foundations #4 — Prototypes
1. What Object.create() Does
Creates a new object and sets its [[Prototype]] to the provided object. Optionally accepts property descriptors:
Loading editor...
2. Implementing Object.create() Polyfill
The simplest polyfill uses a temporary constructor:
Loading editor...
3. What instanceof Does
obj instanceof Constructor checks whether Constructor.prototype appears anywhere in obj's prototype chain:
Loading editor...
4. Implementing instanceof — Walking the Prototype Chain
Loading editor...
5. Symbol.hasInstance — Customizing instanceof
A constructor can override instanceof behavior via Symbol.hasInstance:
Loading editor...
6. isPrototypeOf() — The Other Direction
Instead of obj instanceof Ctor, use proto.isPrototypeOf(obj):
Loading editor...
Key Takeaways
Object.create(proto)uses a temporary constructor to link the new object's__proto__.instanceofwalks the[[Prototype]]chain checking forConstructor.prototype.Symbol.hasInstancelets a function/class customizeinstanceofbehavior.isPrototypeOf()is the equivalent check from the prototype's perspective.- Both
instanceofandisPrototypeOfare O(chain-length) — avoid deep chains in hot paths.
Next: Symbols — Well-Known Symbols & Hidden Properties — every well-known symbol and how they control object behavior.
