← Back to list

JavaScript Classes: An Intuitive Deep Dive

JavaScript classes, introduced in ES6 (ECMAScript 2015), provide a more structured and object-oriented way of defining and creating…

Vallabhaneni Sri Harsha · 2023-09-22 07:52 · 2 claps · 1.9 min read
#javascript #oop-concepts #classes-and-objects #oopjs #typescript
Open on Medium ↗
Wiki topics: CRY · Crypto & Web3 🌐 · Web Development

JavaScript Classes: An Intuitive Deep Dive

JavaScript classes, introduced in ES6 (ECMAScript 2015), provide a more structured and object-oriented way of defining and creating objects. They are, essentially, syntactical sugar over JavaScript’s existing prototype-based inheritance. Here’s a detailed breakdown of JavaScript classes, using the Product class as a practical example.

1. Basics of Classes

At its core, a class is a blueprint or prototype from which you can create objects. Think of it like a mold for a toy — every toy made from that mold will have the same fundamental shape, but might be painted or detailed differently.

class ClassName {
 constructor(parameters) {
 // Initialization code
 }
method1() {
 // …
 }
method2() {
 // …
 }
}

For instance:

class Product {
 constructor(name, price, quantity) {
 this.name = name;
 this.price = price;
 this.quantity = quantity;
}
getTotalPrice() {
 return this.price * this.quantity;
 }
}

2. Instantiating a Class

Once you have a class, you can create a new instance (object) of that class using the new keyword.

let apple = new Product("Apple", 0.5, 100);

3. The Constructor Method

The constructor method in a class is a special method that gets called when you create a new instance. It’s where you’ll typically initialize the properties of your object.

In our Product example:

  • this.name, this.price, and this.quantity are instance properties, initialized using the constructor.

4. Methods

Classes can have other methods to define behaviors:

purchase(quantityToBuy) {
 if (this.quantity < quantityToBuy) {
 console.log("Insufficient stock!");
 return;
 }
 this.quantity -= quantityToBuy;
}

Here, purchase is a method that allows you to buy a certain quantity of the product and adjusts the stock accordingly.

5. The this Keyword

In a class, the this keyword refers to the current instance of the class. It gives you access to the instance’s properties and methods. For instance, in our Product class, this.name, this.price, and this.quantity are references to the instance’s properties.

6. Static Methods and Properties

Static methods and properties are associated with the class itself, not with an instance of the class. They’re useful for values or methods that should be shared across all instances.

class Product {
 static storeName = "JS Mart";
static storeGreeting() {
 console.log(`Welcome to ${this.storeName}!`);
 }
// … rest of the class
}
Product.storeGreeting(); // "Welcome to JS Mart!"

7. Internal Method Calls

Within a class method, you can call another method of the same class using the this keyword:

class Product {
 // … rest of the class
displayStock() {
 console.log(`We have ${this.quantity} of ${this.name} left.`);
 }
purchase(quantityToBuy) {
 // … some logic here
 this.displayStock(); // Call another method of the same class
 }
}

8. Closing Remarks

JavaScript classes offer a structured approach to object-oriented programming in the language. Though they’re syntactical sugar over prototype-based inheritance, they provide a clearer and more intuitive way to define and instantiate objects. Whether you’re working on simple scripts or complex applications, understanding classes is essential to writing robust and maintainable JavaScript code. As you practice and encounter various design challenges, the utility and design patterns related to classes will become more intuitive.


메타데이터
post_id
4dcb640de24
slug
javascript-classes-an-intuitive-deep-dive-4dcb640de24
url
https://medium.com/@vsriharsha814/javascript-classes-an-intuitive-deep-dive-4dcb640de24
canonical_url
https://medium.com/@vsriharsha814/javascript-classes-an-intuitive-deep-dive-4dcb640de24
author_url
https://medium.com/@vsriharsha814
status
ok
fetched_at
2026-07-25 05:01:28