Classes allow a clearer and more concise syntax for creating objects and defining their behavior (compared to traditional constructor functions).
They were introduced in ECMAScript 6 as a more structured and modern way to work with object-oriented programming (and, above all, more similar to other languages).
Classes are Syntactic Sugar for handling objects. That is, it’s a simpler and more comfortable syntax.
But, under the hood, JavaScript objects still have the same behavior and functionalities (and the same peculiarities).
Class Syntax
A class is declared using the keyword class
, followed by the class name. The name must start with an uppercase letter by convention.
class ClassName {
constructor(parameters) {
// Property initialization
}
method() {
// Method definition
}
}
- ClassName: Name of the class, must follow naming conventions in JavaScript.
- constructor(parameters): Special method that runs when a new instance of the class is created. It is used to initialize properties.
- method(): Class methods that define the behavior of the instances.
Let’s see it with an example of how to define and use a basic class:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
}
In this example:
Person
: Is the name of the class.constructor(name, age)
: Initializes the propertiesname
andage
.greet()
: Is a method that prints a greeting with the name and age.this
: Refers to the current instance of the class.
Creating Instances
A class is like a mold or template used to create objects with specific properties and methods. Each object created from a class is called an instance of that class.
To create a new instance based on a class, the keyword new
is used followed by the class name and its arguments, if any.
// Create instances of the Person class
const person1 = new Person("Luis", 30);
const person2 = new Person("Ana", 25);
person1.greet(); // "Hello, my name is Luis and I am 30 years old."
Here,
person1
is an instance ofPerson
, withname
as “Luis” andage
as 30.person2
is an instance ofPerson
, withname
as “Ana” andage
as 25.- When calling the
greet()
method, the greeting is printed.
Methods in Classes
Methods are functions that belong to a class and can be used by its instances. They are defined within the class block.
class Calculator {
add(a, b) {
return a + b;
}
subtract(a, b) {
return a - b;
}
}
const calc = new Calculator();
console.log(calc.add(3, 5)); // Prints: 8
console.log(calc.subtract(10, 4)); // Prints: 6
Difference Between Classes and Literal Objects
While literal objects are useful for simple structures, classes are more suitable for handling complex logic and multiple instances.
Let’s see a comparison of how to create the same object with both syntaxes.
Literal Object
const person = {
name: "Luis",
speak() {
console.log(`Hello, I am ${this.name}.`);
}
};
person.speak(); // Prints: Hello, I am Luis.
Class
class Person {
constructor(name) {
this.name = name;
}
speak() {
console.log(`Hello, I am ${this.name}.`);
}
}
let luis = new Person("Luis");
luis.speak();