100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
JavaScript Foundations for MERN
30 minbeginner

Classes and prototypes

Classes are JavaScript's syntax for creating objects that share structure and behaviour — a blueprint from which many similar objects can be made. They are the familiar face of object-oriented programming in the language, but underneath they rest on JavaScript's distinctive prototype system, and understanding both layers is what makes them fully comprehensible. A class bundles together the data each object will hold and the methods that operate on that data. Creating an object from a class — an instance — gives you an object with its own data but sharing the class's methods. This is the natural way to model entities that come in many similar instances: players, matches, accounts, requests. The crucial thing to understand is that JavaScript classes are, in the words of the specification, largely syntactic sugar over the prototype system that has always underpinned the language. Methods do not live on each instance; they live on a shared prototype object, and instances delegate to it. The class syntax makes this clean and familiar, but the prototype mechanics are what actually happen. Inheritance lets one class build on another, specialising or extending its behaviour, and it is implemented as a chain of prototypes — the prototype chain — along which property lookups travel. Method resolution, the `instanceof` operator, and overriding all follow from this chain. Two topics in this area cause persistent confusion and reward careful study: the behaviour of `this`, which is determined by how a function is called rather than where it is defined, and the distinction between true privacy with `#` fields and the naming conventions that merely suggest it. Together with a sense of when to prefer composition over inheritance, these form the practical core of working with classes.

Analogy🏏Cricket
🏏 Think of it like cricket: Imagine you are following an India vs Australia match but you cannot watch it live — you ask a friend at the Wankhede Stadium to text you the final scorecard. Asking is the asynchronous operation: you do not stand frozen by your phone until the match ends. You go about your day (other code keeps running) and deal with the score when the text arrives. The promise your friend makes — 'I will send you the scorecard when the innings ends' — is exactly a JavaScript Promise: a commitment to deliver a value later. Just as your friend's promise is initially pending (innings in progress), then settles into either fulfilled (they text you '187/4') or rejected (they text 'rain stopped play, no result'), a Promise transitions once from pending to fulfilled or rejected and never flips back. Just as you plan in advance what you will do when the score arrives ('if India scored 180+, I will celebrate; if the feed fails, I will check another source'), `.then()` and `.catch()` register what happens on success and failure. The insight this reveals is that asynchronous programming is not about doing things faster — it is about not standing idle while you wait, so the single JavaScript thread stays free to handle everything else going on.
Lesson 21 of 36
0% complete