JavaScript's Hidden 'new' Keyword: Simplifying Object Creation

Ever felt like JavaScript is just… pretending to be object-oriented? Like you write a function, slap a .prototype on it, and boom - suddenly it's a “class”? 😄 Now enter the "new" keyword. And this is where things get interesting. Because "new" is not just syntax. It’s doing a bunch of hidden work for you. Let’s break it down: You write this: function User(name) { this.name = name; } const user1 = new User("John"); Looks simple, right? But under the hood, JavaScript is doing FOUR steps automatically: - It creates a brand new empty object - It links that object to User.prototype - It sets this inside User to that new object - It returns the object (unless you explicitly return something else) So essentially, "new" is like your invisible assistant. Without new, you'd have to manually do all of this: const obj = {}; Object.setPrototypeOf(obj, User.prototype); User.call(obj, "John"); And honestly… nobody wants to write that every time. - new is not about “creating a class instance” - It’s about orchestrating object creation + prototype linkage + execution context JavaScript doesn’t magically become OOP. It’s still doing what it always does - just giving us a shortcut. #JavaScript #WebDevelopment #Programming #Coding

To view or add a comment, sign in

Explore content categories