Typorithm · medium

instanceof & Object.create

instanceof-operatorinterview-questionsinstanceofobject-createprototype-chain
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn instanceof & Object.create

0%

Prototypes & Classes

obj instanceof Ctor is true when Ctor.prototype appears anywhere in obj's prototype chain. Object.create(proto) makes a new object whose [[Prototype]] is proto, and Object.getPrototypeOf reads that link back. instanceof simply walks the chain looking for a match.

The instanceof operator answers a prototype-chain question: obj instanceof Ctor is true exactly when Ctor.prototype is found somewhere in obj's chain. That is why arr instanceof Array and arr instanceof Object are both true — both prototypes are on the chain.

Object.create(proto) builds a brand-new object whose internal [[Prototype]] is set directly to proto, with no constructor involved. It is the most explicit way to choose an object's prototype, and Object.getPrototypeOf(obj) reads that exact link back.

Because instanceof only checks whether a constructor's .prototype is in the chain, an object created with Object.create still counts as an instanceof Object as long as Object.prototype is reachable up the chain.

01

instanceof is true when Ctor.prototype is in obj's chain.

02

Object.create(proto) makes proto the new object's [[Prototype]].

03

Object.getPrototypeOf reads that link back.

const arr = [1, 2, 3]
arr instanceof Array  // true — Array.prototype is in arr's chain
arr instanceof Object // true — Object.prototype is further up
const proto = { greet() { return "hi" } }
const obj = Object.create(proto)
Object.getPrototypeOf(obj) === proto // true
obj instanceof Object // true

Read the implementation

Connect each part of the code to the concept before you type.

1. instanceof checks the chain

instanceof is true because Array.prototype appears in arr's prototype chain.

arr instanceof Array  // true — Array.prototype is in arr's chain

2. Further up the chain

Object.prototype sits higher in the chain, so arr is also an instance of Object.

arr instanceof Object // true — Object.prototype is further up

3. Object.create sets the prototype

Object.create(proto) makes a new object whose [[Prototype]] is exactly proto.

const obj = Object.create(proto)

4. Confirm the link

Object.getPrototypeOf confirms obj's [[Prototype]] is the proto we passed in.

Object.getPrototypeOf(obj) === proto // true

5. Still an Object

proto itself inherits from Object.prototype, so obj is an instance of Object.

obj instanceof Object // true

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

instanceof & Object.create: Lesson & Practice · Typorithm | ApplyDjinn