Welcome to Classes in JavaScript

In this lesson, we will explore the fundamentals of classes in JavaScript.

Start learning
Classes

What are Classes?

Classes in JavaScript are a way to create objects with shared properties and methods. They provide a blueprint for creating multiple instances of similar objects.

// syntax
class ClassName
{ // code goes here
}

Class instatiation

Create an object from a class

// syntax
const objectName = new ClassName();

Class Methods

In class we can create class methods. Methods are JS functions inside the class. Let us create some class methods.

class Person{
greet(){
console.log("Hello!");
}
}
console.log(Person.greet());

Class Constructor

A function which allow us to create a blueprint for objects

// syntax
class ClassName {
constructor(param1, param2) {
this.param1 = param1;
this.param2 = param2;
}
}

We can add more propierties and default values to our constructor

Take a Breath

..Ok?

Just calm down and take a moment to relax.
Let's go Again!

Different types of methods

A method coluld be regular method or a getter or a setter.

Getter

The get method allow us to access value from the object. We write a get method using keyword get followed by a function. Instead of accesing properties directly from the object we use getter to get the value.


class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
get getName() {
return this.name;
}
}
const person1 = new Person("Alice", 30);
console.log(person1.getName); // Output: Alice

Setter

The setter method allow us to modify the value of certain properties. We write a setter method using keyword set followed by a function.

class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
set setAge(newAge) {
this.age = newAge;
}
}
const person1 = new Person("Alice", 30);
person1.setAge = 31;
console.log(person1.age); // Output: 31

Static Method

The static keyword defines a static method for a class. Static method are not called on instances of the class. Instead, they are called on the class itself. These are often utility functions, such as functions to create or clone objects. An example of static method is Date.now(). The now method is called directly from the class.

Inheritance

Using inheritance we can access all the properites and the methods of the parent class. This reduces repetition fo code. If you remember, we have a Person parent class and we will create children from it.รง Our children class could be student, teach, etc..

// syntax class ChildClassName extends { // code goes here }

Overriding

If we want to customize, the methods and if we want to add extra properties, we need to use the constructor function the child class too. Inside the constructor function we call the super() function to access all the properties from the parent class. The Person class didn't have gender but now let us give gender property for the child class, Student. If the same method name used in the child class, the parent method will be overridden