Low-Code Development Platform utalic

Classes et héritage de classes en JavaScript

Classes and Class Inheritance

JavaScript is a versatile and dynamic programming language, and one of its most powerful features are the ability to create and inherit classes. 
Classes in JavaScript provide a way to structure and organize code, 
making it easier to write and maintain large-scale applications. In this post, we will explore the basics of classes and class inheritance in JavaScript.
Classes in JavaScript essentially just function that act as blueprints for objects. Objects created from these classes are known as instances, and they inherit the properties and methods defined in the class. Classes in JavaScript are defined using the class keyword, followed by a class name and a block of code that defines the properties and methods of the class.

Here is an example of a simple class in JavaScript:

				
					class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }
  sayHello() {
    console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
  }
}
				
			

In this example, we have defined a class called Person, which has two properties, name, and age, and one method, sayHello(). The constructor is a special method that is automatically called when a new instance of the class is created. In the constructor, we assign the values of the name and age properties passed as arguments to this keyword, which refers to the instance of the class.

Once we have defined a class, we can create instances of it using the new
keyword. Here is an example:

				
					const john = new Person('John Doe', 30);
john.sayHello();
// Output: Hello, my name is John Doe and I am 30 years old.
				
			

Class inheritance is a powerful concept in object-oriented programming, 
and it allows us to create new classes that inherit properties and methods from existing classes. In JavaScript, class inheritance is achieved using the extends keyword. Here is an example:

				
					class Student extends Person {
  constructor(name, age, major) {
    super(name, age);
    this.major = major;
  }
  sayHello() {
    console.log(`Hello, my name is ${this.name} and I am ${this.age} 
years old. I am studying ${this.major}.`);
  }
}
				
			
In this example, we have defined a new class called Student, which extends the Person class. This means that the Student class inherits all the properties and methods of the Person class, and we can add new properties and methods specific to the Student class. In the constructor of
the Student class, we call the super keyword, which refers to the parent class (Person), and passes the name and age arguments to the parent class’s constructor. This allows us to initialize the name and age properties in the parent class.
We can now create instances of the Student class and call its methods:
 
				
					onst jane = new Student('Jane Doe', 25, 'Computer Science');
jane.sayHello();
// Output: Hello, my name is Jane Doe and I am 25 years old.
// I am studying Computer Science.
				
			

Conclusion

Classes and class inheritance are powerful features in JavaScript that provide a way to structure and organize code. They allow us to create objects that inherit properties and methods from other objects

Facebook
Twitter
LinkedIn
WhatsApp