I’ve lost count of how many times I’ve accidentally called a class like a normal function and watched my code explode.
Click here to read for free
It’s one of those silent failures you may encounter in JavaScript. It gives no compile-time warning, just a runtime crash that leaves you irritated.
Yesterday going through some articles I came across a meta-property that’s been hiding in plain sight since ES6. It is elegantly solving problems like these and I didn’t even know it.
Most developers I know have never heard of it, but it’s a game-changer for building robust class hierarchies and preventing those hidden instantiation bugs that slip through code reviews.
Let’s get started with the action.
The Sneaky Problem Causing Bugs
Let’s start with understanding the problem that we are going to fix today. It is faced by many developers and doesn’t get caught in the code reviews.
Suppose you write a beautiful constructor function like below:
function User(name, email) {
this.name = name;
this.email = email;
this.isActive = true;
}
Learn more about The JavaScript Bug That Slips Through Code Reviews (And How to Catch It)