Interview question
How does the instanceof operator work and what are its limitations? instanceof operator कैसे काम करता है और इसकी limitations क्या हैं?
Answer
instanceof checks whether a constructor's prototype exists anywhere in an object's prototype chain. It works well for standard scenarios but has edge cases worth knowing.
class Animal {}
class Dog extends Animal {}
const rex = new Dog();
console.log(rex instanceof Dog); // true
console.log(rex instanceof Animal); // true - checks the whole chain
console.log(rex instanceof Object); // true - everything inherits from Object
// LIMITATION 1: doesn't work reliably across different execution contexts (iframes)
// An array created in one iframe won't be instanceof Array in another iframe
// because each context has its own separate Array constructor
// LIMITATION 2: primitives are NOT instances of their wrapper classes
console.log(5 instanceof Number); // false - primitive number
console.log(new Number(5) instanceof Number); // true - boxed Number object
typeof 5; // 'number'
typeof new Number(5); // 'object' - different behavior entirely
// LIMITATION 3: instanceof can be manipulated via Symbol.hasInstance
class EvenNumber {
static [Symbol.hasInstance](num) {
return Number.isInteger(num) && num % 2 === 0;
}
}
console.log(4 instanceof EvenNumber); // true - custom instanceof logic!
console.log(5 instanceof EvenNumber); // false
// LIMITATION 4: manually reassigning prototype breaks instanceof checks
function Foo() {}
const foo = new Foo();
console.log(foo instanceof Foo); // true
Foo.prototype = {}; // reassigning after instance creation
console.log(foo instanceof Foo); // false - foo's prototype link is unchanged, but Foo.prototype moved
// Alternative: for checking plain object 'type' by structure, consider duck typing
// or Object.prototype.toString.call() for primitives/built-ins
console.log(Object.prototype.toString.call([])); // '[object Array]'
console.log(Object.prototype.toString.call(null)); // '[object Null]'instanceof यह check करता है कि constructor का prototype किसी object की prototype chain में कहीं है या नहीं। Standard scenarios में अच्छे से काम करता है पर कुछ edge cases हैं।
class Animal {}
class Dog extends Animal {}
const rex = new Dog();
console.log(rex instanceof Dog); // true
console.log(rex instanceof Animal); // true - पूरी chain check होती है
console.log(rex instanceof Object); // true
// LIMITATION 1: अलग iframes में reliably काम नहीं करता
// एक iframe में बना array दूसरे iframe में instanceof Array नहीं होगा
// LIMITATION 2: primitives अपने wrapper classes के instance नहीं हैं
console.log(5 instanceof Number); // false
console.log(new Number(5) instanceof Number); // true
// LIMITATION 3: Symbol.hasInstance से manipulate हो सकता है
class EvenNumber {
static [Symbol.hasInstance](num) {
return Number.isInteger(num) && num % 2 === 0;
}
}
console.log(4 instanceof EvenNumber); // true - custom logic!
console.log(5 instanceof EvenNumber); // false
// LIMITATION 4: prototype manually reassign करने से instanceof टूट सकता है
function Foo() {}
const foo = new Foo();
console.log(foo instanceof Foo); // true
Foo.prototype = {};
console.log(foo instanceof Foo); // false
// Alternative: primitives/built-ins के लिए
console.log(Object.prototype.toString.call([])); // '[object Array]'Was this answer clear?