Understanding JavaScript prototype property and object inheritance

I’ve been coding in JavaScript for a while but still struggle with the prototype concept. Can someone explain how prototypes work in JavaScript?

function Car() {}
Car.prototype.drive = function() { console.log('Driving!'); };
var myCar = new Car();
myCar.drive();

From what I understand, JavaScript doesn’t have traditional classes like other languages. Instead, it uses objects and prototype-based inheritance. But I’m confused about how the .prototype property actually works when creating new object instances.

What exactly happens when you add methods to a constructor’s prototype? How does this relate to creating new objects with the new keyword?

yep, totally! when you use new Car(), it connects myCar to Car.prototype. it checks if drive is there on myCar, if not, it looks up to the prototype. all instances can use it without making copies, pretty cool!

Think of the prototype as a shared blueprint that all instances can reference. When you define Car.prototype.drive, you’re basically saying “every Car object should have access to this method, but don’t copy it to each instance.” Here’s where it gets cool - when you call new Car(), JavaScript creates a hidden link between your new object and Car.prototype. That link powers the lookup chain. Call myCar.drive() and JavaScript first checks if drive exists directly on myCar. It doesn’t, so it follows the prototype chain and finds the method on Car.prototype. This saves memory since the method exists only once, not on every instance. You can even add methods to the prototype after creating instances, and they’ll immediately have access to those new methods.

People get confused because they think the prototype is just a static template. It’s not - it’s a live reference. When you create a constructor function like Car, JavaScript automatically creates a prototype object and links them together. The .prototype property is where you store methods and properties that all instances can share. When you use new Car(), JavaScript connects your new object’s __proto__ to Car.prototype. So your instance can use methods like drive without actually owning a copy. You can even add methods to the prototype later, and existing instances will have access to them right away. This dynamic reference system is what makes JavaScript inheritance different from classical inheritance.