Engrain by ByteRush

JS.04.02 Junior· 10 minCore — asked often

What new really does: constructors, instanceof and Object.create

new creates an object, links it to the constructor's prototype, runs the constructor, and returns the object. Build new and instanceof yourself.

Comes up in: JavaScript, Coding

Learn#

Aditya forgets new and writes const u = User("Asha"). In strict code he gets a TypeError. In an old sloppy script, name silently lands on the global object, and u is undefined. Knowing what new does explains both.

A constructor is a function meant to be called with the new operator. new User("Asha") does four things:

  1. Creates an empty object.
  2. Links it to User.prototype.
  3. Calls User with this set to that object.
  4. Returns the object — unless User returned an object of its own.

Returning a primitive from a constructor is ignored; returning an object replaces the new one.

User.prototype.constructor points back to User, so every instance can find its constructor.

function User(name) { this.name = name; }
User.prototype.hi = function () { return "Hi " + this.name; };
const u = new User("Asha");
console.log(u.hi(), Object.getPrototypeOf(u) === User.prototype, u.constructor === User);

function Weird() { this.a = 1; return { b: 2 }; }
function Prim() { this.a = 1; return 42; }
console.log(JSON.stringify(new Weird()), JSON.stringify(new Prim()));
console.log(u instanceof User, u instanceof Object, [] instanceof User);

Output (verified)

Hi Asha true true
{"b":2} {"a":1}
true true false

instanceof walks the value’s prototype chain looking for User.prototype.

The four steps of newStep 1 of 4

new object

  • { }

its prototype

  • —

this inside User

  • —

Step 1: new makes an empty object.

Read the steps as text
  1. Step 1: new makes an empty object.
  2. Step 2: it links the object to User.prototype, where hi() lives.
  3. Step 3: it calls User with this = the new object. The body adds name.
  4. Step 4: User returned nothing, so new returns the object.

Predict#

Predict first

function A() {} const a = new A(); A.prototype = { fresh: true }; Is a instanceof A now?

Interview angle#

Say it like this — about 30 seconds

“new F(args) creates an empty object and sets its prototype to F.prototype. It calls F with this bound to that object, then returns it — unless F returns an object, which wins. instanceof walks the left side’s prototype chain looking for F.prototype. Object.create(proto) does the linking step without calling a constructor, and Object.create(null) gives an object with no prototype at all — a clean dictionary.”

Stays on your device. Nothing is uploaded.
Follow-up Are classes just syntactic sugar over constructor functions?
Mostly, but not only. Classes cannot be called without new. Their bodies are strict, and they are in the TDZ before their line. Methods are not enumerable. Derived classes must call super() before using this.
Follow-up Implement instanceof.
Get the value’s prototype; loop: if it equals F.prototype return true; move to its prototype; stop at null and return false. Primitives return false.
Follow-up Why use Object.create(null)?
For dictionaries: no inherited keys like toString or __proto__, so any string is safe as a key.

Practice#

Timed, like the real round

Implement myNew and myInstanceof in 20 minutes, without using new or instanceof for the main logic.

Done when

  • myNew(User, "Asha") returns an object linked to User.prototype with name set; an object returned by the constructor wins, a primitive does not.
  • myInstanceof matches instanceof for instances, arrays, Object.create(null) and primitives.
Your solution
Ctrl/⌘ + Enter

Remember#

Try to answer each card before you open it. Then add them to your reviews.

What are the four steps of new F()?

Create an object, link it to F.prototype, call F with this = it, return it (unless F returns an object).

How does instanceof work?

It walks the value's prototype chain looking for F.prototype.

What does Object.create(null) give you?

An object with no prototype — a clean dictionary.

What happens if a constructor returns an object?

new returns that object instead of the newly created one.

Go deeper

Four layers. Stop at the one your interview needs; each ends with a quick check.

The core ideaEvery round

new = create, link to F.prototype, run F on it, return it. instanceof = is F.prototype somewhere in the chain?

Check yourself: What does instanceof compare against?

The constructor's current prototype property.

How it works underneathSenior rounds

new calls the function’s [[Construct]], which uses OrdinaryCreateFromConstructor to make the object from F.prototype Source: ECMA-262: OrdinaryCreateFromConstructor. instanceof first checks F[Symbol.hasInstance], then falls back to walking the chain Source: ECMA-262: InstanceofOperator.

Check yourself: What does instanceof check first, before walking the chain?

Whether the right side has a Symbol.hasInstance method.

Edge cases and trapsWhere people slip

Each window or iframe has its own built-ins. So an array from an iframe fails instanceof Array; use Array.isArray. Object.create(proto, descriptors) also takes property descriptors, like { id: { value: 1, writable: false } } (JS.06.01).

Check yourself: Why can instanceof Array fail for an array from an iframe?

Each window or iframe has its own Array.prototype.

In productionStaff-level answers

For user-controlled keys, prefer Map, or Object.create(null), so keys like __proto__ or constructor cannot collide with inherited ones. Use Array.isArray and duck typing across realms, and classes (JS.04.03) instead of hand-written constructor functions in new code.

Check yourself: Name one safe alternative to a plain object as a dictionary.

Map, or Object.create(null).

Finished the five steps? Mark it, and its cards join your reviews.

Last checked 2026-10-31. Spot a mistake? Use “Report an error” below — fixes are listed on the corrections page.

Interview questions this answers

  • What exactly does the new keyword do?
  • Are JavaScript classes just syntactic sugar? What is different from constructor functions?
  • How does instanceof work? Implement it.
  • What does Object.create(null) give you and why use it?