this in Constructors and the new Keyword in JavaScript
Calling a function with new creates a fresh object as this. Here is how constructors work.
this in Constructors and the new Keyword in JavaScript
When you call a function with new, JavaScript creates a brand-new object and sets this to it. Here is how constructors work.
What new Does
- Creates a new empty object.
- Sets
thisto that new object inside the function. - Runs the function body (which typically adds properties to
this). - Returns
this(the new object), unless the function returns its own object.
function Person(name, age) { this.name = name; this.age = age; } const p = new Person("Kunal", 30); console.log(p.name); // "Kunal" console.log(p.age); // 30 `` ### Forgetting new Is a Bug ```js const p = Person("Kunal", 30); // no new console.log(p); // undefined console.log(window.name); // "Kunal" (polluted global!) `` Without `new`, `this` is the global object (or `undefined` in strict mode). Properties get added to `window` instead of a new object. ### ES6 Classes Require new ```js class Person { constructor(name) { this.name = name; } } const p = Person("Kunal"); // TypeError: Class constructor cannot be invoked without 'new' `` ### new Binding Precedence `new` overrides `bind` for `this` (but preset arguments remain): ```js const BoundPerson = Person.bind(null, "Kunal"); const p = new BoundPerson(30); // this is a new object, not null `` ### Returning from a Constructor ```js function Person(name) { this.name = name; return { custom: true }; // returns this object, not this } const p = new Person("Kunal"); console.log(p.custom); // true (returned object) console.log(p.name); // undefined (not this) `` If you return an object, that object is returned instead of `this`. If you return a primitive (or nothing), `this` is returned. ### The Takeaway `new` creates a fresh object and sets `this` to it. Forgetting `new` with a function constructor pollutes the global object. ES6 classes throw if you forget `new`. `new` overrides `bind` for `this`. Returning an object from a constructor returns that object instead of `this`.
Creates a new empty object, sets this to that object inside the function, runs the function body, and returns this (unless the function returns its own object).
this becomes the global object (or undefined in strict mode). Properties get added to window instead of a new object. The function returns undefined. This is a common bug with function constructors.
Yes. Calling a class constructor without new throws TypeError: Class constructor cannot be invoked without 'new'. This prevents the forgotten-new bug.
Yes. If you call a bound function with new, this is a new object, not the bound this. However, any preset arguments from bind still apply to the new object.
The returned object is used instead of this. If you return a primitive (number, string) or nothing, this is returned. This is why constructors should usually not return anything.
Ready to master React completely?
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

