In this tutorial, we will delve into creating classes and objects in JavaScript. The primary goal is to equip you with the knowledge to define and create your own classes, and instantiate objects from them in a real-world context.
By the end of this tutorial, you will learn:
The prerequisites for this tutorial are a basic understanding of JavaScript syntax and knowledge of fundamental programming concepts such as variables, functions and control flow.
In JavaScript, classes are a template for creating objects. They encapsulate data with code to manipulate that data. Classes in JS are built on prototypes but also have some syntax and semantics that are not shared with ES5 class-like semantics.
An object is an instance of a class. When a class is defined, no memory is allocated but when it is instantiated (i.e., an object is created) memory is allocated.
To define a class in JavaScript, you use the class
keyword, followed by the name of the class with a pair of curly braces {}
.
class MyClass {
// class methods and properties go here
}
After a class is defined, you can use the new
keyword to create an object from the class.
let obj = new MyClass();
class Car {
constructor(brand) { // Constructor method is a special method for creating and initializing an object
this.carBrand = brand; // this keyword refers to the object it belongs to
}
present() { // Class method
return `I have a ${this.carBrand}`;
}
}
let myCar = new Car("Toyota");
console.log(myCar.present()); // Logs: "I have a Toyota"
In this example, Car
is a class and myCar
is an object of the Car
class. The Car
class has a constructor which accepts a brand
parameter and a method present
to display the car's brand.
class Car {
constructor(brand) {
this.carBrand = brand;
this.year = new Date().getFullYear(); // Current year
}
age() {
return `This ${this.carBrand} is ${new Date().getFullYear() - this.year} years old.`;
}
}
let myCar = new Car("Toyota");
console.log(myCar.age()); // Logs: "This Toyota is 0 years old."
In this example, we added another property year
to the Car
class and a method age
to calculate the car's age.
In this tutorial, you have learned the basics of creating classes and objects in JavaScript. You have also learned how to define methods and properties for a class and how to instantiate an object from a class.
The next steps in your learning could be understanding inheritance in JavaScript, and how classes can be extended. You may also wish to learn more about other features of ES6, such as arrow functions and promises.
Define a class Rectangle
that accepts width
and height
as parameters. Add a method area
to calculate the area of the rectangle.
Create an instance of Rectangle
and calculate the area.
Solution
class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}
area() {
return this.width * this.height;
}
}
let myRectangle = new Rectangle(5, 6);
console.log(myRectangle.area()); // Logs: 30
In this solution, we've created a Rectangle
class with width
and height
properties. We've also added a method area
that returns the product of width
and height
. When we create an instance of Rectangle
and call the area
method, it logs the area of the rectangle.