Skip to main content

Command Palette

Search for a command to run...

What Really Happens When You Write new Person()?

Updated
6 min readView as Markdown
What Really Happens When You Write new Person()?
S
I'm a passionate software engineer and full-stack MERN developer who loves to turn ideas into scalable, user-centric applications. I have hands-on experience in building modern web solutions using React, Node.js, Express.js, MongoDB, following clean architecture and best development practices. My experience in the Cognizant Healthcare Product Consulting (HPC) program has given me hands-on exposure to SQL, PL/SQL, U.S. healthcare payer systems and TriZetto Facets, and has helped me to further develop my skills in working with enterprise software in domain-driven environments. I enjoy tackling complex technical problems, constantly learning, and building reliable applications that deliver business value.

How does new Person() actually build an object?

Many beginners think new is just special syntax that "creates an object," the same way {} does. That's not true.

new is an operator. It takes an ordinary function and runs a four-step process behind the scenes - one of those steps is creating the object, but that's only a quarter of the story.

  1. new creates a fresh object,

  2. links it to the constructor's prototype,

  3. runs the constructor with this pointing at that object, and

  4. returns it.


Analogy: The School Admission Office

Think of admission day at a school.

  • Constructor function = the blank admission form template

  • new keyword = the clerk who processes the form

  • this = the specific blank form being filled right now

  • Prototype = the school's official seal, kept once in the office

  • Instance = the completed, stamped ID card handed back to you

Every student gets their own filled-in form. Nobody gets their own personal seal - they all point back to the one seal in the office.


What happens, step by step

When you write new Person("Aisha", 21), JavaScript does this in order:

  1. Creates a new, empty object: {}

  2. Links that object's prototype to Person.prototype

  3. Binds this to the new object and runs Person's code

  4. Returns the object automatically, unless the constructor explicitly returns a different object

function Person(name, age) {
  this.name = name;
  this.age = age;
}

const aisha = new Person("Aisha", 21);
console.log(aisha.name); // Aisha

Run that in your browser console or a Node.js environment - no setup needed.


Why do we even need new?

Without it, you'd write out a fresh object literal by hand for every single student, copying the same structure over and over.

new automates that repetition. One template, unlimited stamped cards.


Important Rule

Forget the new keyword, and the clerk never shows up. this doesn't get bound to a fresh object - in non-strict mode it silently falls back to the global object.

function Person(name) {
  this.name = name;
}

const broken = Person("Ravi"); // no `new`!
console.log(broken);     // undefined
console.log(window.name); // "Ravi" — leaked onto the global object

This is the single most common constructor - function bug beginners hit. We're skipping Reflect.construct and other advanced - call tricks for now - confidence with the basic flow comes first.


Now that we know how new fills out one form, the next question is: how does that form know which stamp to use?

How does the instance know which methods it can use?

A prototype is a shared object that every instance can reach through a lookup chain, without owning a personal copy.

Going back to the admission office: the seal isn't reprinted onto every card. Each card just points back to the one master seal the office keeps.

Person.prototype.greet = function () {
  return `Hi, I'm ${this.name}`;
};

aisha.greet(); // "Hi, I'm Aisha"

aisha doesn't have greet sitting on itself. JavaScript walks up to Person.prototype, finds it there, and uses it.

Where the method lives Memory per instance Shared across instances?
Defined inside the constructor (this.greet = ...) One copy each No
Defined on the prototype Zero extra copies Yes

Note

Put shared behavior on the prototype, not inside the constructor. A thousand students shouldn't carry a thousand copies of the same greet function.


We've covered how objects get created and how they reach shared behavior. Here's the whole flow, start to finish.

Putting it all together

function Car(brand, year) {
  this.brand = brand;
  this.year = year;
}

Car.prototype.honk = function () {
  return `${this.brand} says beep!`;
};

const swift = new Car("Maruti Swift", 2022);
const nexon = new Car("Tata Nexon", 2023);

swift.honk(); // "Maruti Swift says beep!"
swift.honk === nexon.honk; // true — same function, shared via prototype
Aspect Calling Car() plainly Calling new Car()
New object created? No Yes
this binding Global object (or undefined in strict mode) The new object
Prototype linked? No Yes, to Car.prototype
Return value Whatever the function returns (usually undefined) The new object, automatically

Assignment

Create a constructor function Student with name and grade properties. Add a study method on its prototype. Create two Student instances and confirm they share the same study function reference.

Click to see the solution
function Student(name, grade) {
  this.name = name;
  this.grade = grade;
}

Student.prototype.study = function () {
  return `${this.name} is studying for grade ${this.grade}`;
};

const priya = new Student("Priya", 10);
const arjun = new Student("Arjun", 10);

console.log(priya.study === arjun.study); // true

Both instances point to the exact same study function on Student.prototype - nothing was duplicated.


Conclusion

  • new runs a four-step process: create, link, bind, return

  • A constructor function is just a normal function used as a template

  • Object creation happens automatically inside step one of new

  • Prototype linking is what lets instances share methods without copying them

  • Instances are the individual objects new hands back to you

If this felt like a lot happening behind one keyword, that's okay. What matters is understanding the flow, not memorizing every internal step on day one.


One housekeeping note for readers following the series in order: this article explains the exact mechanism that JavaScript's class syntax quietly runs under the hood. If you read the OOP/Classes post first, it's worth circling back here afterward - class is a template over the new + prototype flow covered above, not a separate concept.

If you find this helpful, drop a comment or reaction.

More from this blog