Implement instanceof
instanceofinstanceof: first require a callable right-hand side with an object/function prototype, then return false for a null or primitive left-hand side, and otherwise walk toward Constructor.prototype. Invalid ordinary constructor inputs throw TypeError. Custom Symbol.hasInstance dispatch and bound-function internals are outside this exercise, so this is not a complete replacement for the operator.Arguments
obj: any —The value to check (may be an object or a primitive). Constructor: Function —The constructor function whose .prototypeshould be searched in the prototype chain.
Returns
boolean — true if Constructor.prototype is in obj's prototype chain; otherwise false.Solution idea
For an ordinary constructor, the core prototype test asks whether Constructor.prototype appears in the left value's prototype chain. Validate this helper's callable-RHS and object/function-prototype contract before returning false for primitive left values, then repeatedly call Object.getPrototypeOf until the target or null. Native instanceof also dispatches custom Symbol.hasInstance methods and has special bound-function behavior; both are explicitly outside this scoped exercise.
// Example 1
function Person() {}
const p = new Person();
myInstanceOf(p, Person); // => true
// Example 2
myInstanceOf({}, Array); // => false
myInstanceOf([], Array); // => true
// Example 3: primitives
myInstanceOf(1, Number); // => false
myInstanceOf('a', String); // => false
myInstanceOf(null, Object); // => falseSolution
Overview
For an ordinary constructor, the core prototype test asks whether Constructor.prototype appears in the left value's prototype chain. Validate this helper's callable-RHS and object/function-prototype contract before returning false for primitive left values, then repeatedly call Object.getPrototypeOf until the target or null. Native instanceof also dispatches custom Symbol.hasInstance methods and has special bound-function behavior; both are explicitly outside this scoped exercise.
Approach: Prototype chain traversal (recommended)
First validate the documented ordinary-constructor contract: the right-hand side must be callable and its prototype must be an object or function. Throw TypeError for either invalid case. Then return false for a null or primitive left value. For an object/function left value, set target = Constructor.prototype and walk upward with Object.getPrototypeOf; return true on identity with the target and false at null. This runs in O(depth) time and O(1) space. It intentionally does not implement custom Symbol.hasInstance or bound-function forwarding.
export default function myInstanceOf(obj, Constructor) {
if (typeof Constructor !== 'function') {
throw new TypeError('myInstanceOf: Constructor must be a function');
}
const targetProto = Constructor.prototype;
if (targetProto === null || (typeof targetProto !== 'object' && typeof targetProto !== 'function')) {
throw new TypeError('myInstanceOf: Constructor must have an object prototype');
}
if (obj === null || obj === undefined) return false;
const t = typeof obj;
if (t !== 'object' && t !== 'function') return false;
let current = Object.getPrototypeOf(obj);
while (current !== null) {
if (current === targetProto) return true;
current = Object.getPrototypeOf(current);
}
return false;
}
export default function myInstanceOf(obj: any, Constructor: Function): boolean {
if (typeof Constructor !== 'function') {
throw new TypeError('myInstanceOf: Constructor must be a function');
}
const targetProto: any = (Constructor as any).prototype;
if (targetProto === null || (typeof targetProto !== 'object' && typeof targetProto !== 'function')) {
throw new TypeError('myInstanceOf: Constructor must have an object prototype');
}
if (obj === null || obj === undefined) return false;
const t = typeof obj;
if (t !== 'object' && t !== 'function') return false;
let current: any = Object.getPrototypeOf(obj);
while (current !== null) {
if (current === targetProto) return true;
current = Object.getPrototypeOf(current);
}
return false;
}
Notes & Pitfalls
Checking obj.__proto__directly is non-standard and can be missing; useObject.getPrototypeOf.Validate the callable right-hand side and its object/function prototype before the primitive-left early return required by this helper's contract. Invalid ordinary constructor inputs throw TypeError instead of silently returning false. This does not implement custom Symbol.hasInstancebehavior or bound-function forwarding.
Functions are objects too: myInstanceOf(function(){}, Function)should betrue.Objects created with Object.create(null)have anullprototype chain; should generally returnfalsefor most constructors.Constructor.prototypecan be replaced after object creation; the helper checks the current prototype reference.A callable whose prototype is null, undefined, or primitive is rejected by this ordinary-constructor contract.
Prototype chain traversal with Object.getPrototypeOf.Validate the ordinary constructor before guarding nulland primitive left values.Compare by reference equality ( ===) on prototypes.
Common mistakes on this challenge
myInstanceOf should walk prototype chain with strict guards
Correct behavior depends on constructor validation and iterative prototype traversal. Before inspecting the left value, throw TypeErrorunless the right-hand side is callable with an object/function prototype.After that right-hand-side validation, return false for primitives, null, andundefined.Walk Object.getPrototypeOf(obj)chain and compare againstConstructor.prototype.
Resources
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.