javascript

Javascript: Working With Classes

Introduced in ES6 as a major update to the Object Oriented Programming concept in Javascript, Classes are templates for creating objects.

Classes can also referred to as the “syntactic sugar” of constructor functions. This means that they are an alternative way of writing constructor functions.

Working With a Class

One way to define a class is to use the class declaration. To declare a class, you use the “class” keyword followed by the name of the class.

class Rectangle{
}

Using constructor Method

A constructor method is a special method created within a declared class.

Constructor methods are used to initialize object properties created within that class.

There can only be one special method with the name “constructor” within a class. A syntaxError will be thrown if a class contains more than one instance of a constructor method.

class Rectangle{
constructor(width, height){
this.width = width;
this.height = height;
}
}

Class Methods

class methods can be referred to as a special method within your declared class.

This special class manipulates, interacts with or showcase the object properties created with the constructor method.

For instance, after declaring the properties “width” and “height” of the Rectangle class, it is possible that we also want to interact with these declared properties and perform some kind of calculation with them.

Also, after creating this special method that performs the calculation, we might also want to create another method that calls this method so that the result can be seen when it is called through the class instance.

class Rectangle{
constructor(width, height){
this.width = width;
this.height = height;
}

//Method
calcArea(){
return this.width * this.height;
}

//Getter
getArea(){
return this.calcArea();
}
}

Just writing the code like this and running it won’t give any result.

We need to now create an instance of this our “Rectangle” class so that we can showcase the logic that we have created.

We create an instance like so …

const rectangle = new Rectangle(10,10);
console.log(rectangle.getArea()); //100

Full Working Code:

class Rectangle{
constructor(width, height){
this.width = width;
this.height = height;
}

//Method
calcArea(){
return this.width * this.height;
}

//Getter
getArea(){
return this.calcArea();
}
}

const rectangle = new Rectangle(10,10);
console.log(rectangle.getArea()); //100

Recent Posts

Google Announces that AI-developed Drug will be in Trials by the End of the Year

Isomorphic Labs, a drug discovery start-up launched four years ago and owned by Google’s parent…

5 hours ago

Instagram Extends Reels Duration to 3 Minutes

Regardless of whether TikTok faces a U.S. ban, Instagram is wasting no time positioning itself…

2 days ago

AWS Expands Payment Options for Nigerian Customers, Introducing Naira (NGN) for Local Transactions

Amazon Web Services (AWS) continues to enhance its customer experience by offering more flexible payment…

6 days ago

Why JavaScript Remains Dominant in 2025

JavaScript, often hailed as the "language of the web," continues to dominate the programming landscape…

1 week ago

Amazon Moves to Upgrade Alexa with Generative AI Technology

Amazon is accelerating efforts to reinvent Alexa as a generative AI-powered “agent” capable of performing…

1 week ago

Smuggled Starlink Devices Allegedly Used to Bypass India’s Internet Shutdown

SpaceX's satellite-based Starlink, which is currently unlicensed for use in India, is reportedly being utilized…

1 week ago