Introduction to JavaScript Classes
JavaScript classes are a feature introduced in the ECMAScript 2015 specification of JavaScript. This feature allows developers to create objects using a simple and familiar syntax. JavaScript classes provide a much simpler and clearer syntax to create objects and deal with inheritance than the previous prototype-based inheritance model. This tutorial will provide an overview of how to use JavaScript classes, their features and benefits.
Creating a Class
Creating a class in JavaScript is very simple. The class keyword is used to declare a class. The class can have properties and methods. The properties are just like variables and the methods are just like functions. Here is an example of a class that defines a Person object:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
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.");
}
}
[/dm_code_snippet]
The constructor() method is a special method for creating and initializing an object created with a class. This method is called when an object is created from the class. The constructor method is optional, but if it is present it must be written as shown above. The constructor method is used to set up the initial state of the object. In this example, it takes two parameters, name and age, and sets the instance properties this.name and this.age to the values passed in as parameters.
The greet() method is an example of a class method, which is a method associated with a class. Methods are defined the same way as functions, but they are written inside the class definition. This method uses the this keyword to access the object’s name and age properties, and prints a greeting to the console.
Creating an Object from a Class
To create an object from a class, we use the new keyword followed by the class name. This will create a new object with the properties and methods defined in the class. Here is an example of creating an object from the Person class:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
let person = new Person("John", 30);
person.greet(); // outputs "Hello, my name is John and I am 30 years old."
[/dm_code_snippet]
The new keyword creates a new instance of the Person class and calls the constructor method with the parameters we provided. This sets the name and age of the new object. We then call the greet() method to print out a greeting to the console.
Class Inheritance
Class inheritance is a way to share methods and properties between classes. This allows us to create a parent class with methods and properties that can be inherited by child classes. Here is an example of a class inheritance:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
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.");
}
}
class Student extends Person {
constructor(name, age, subject) {
super(name, age);
this.subject = subject;
}
study() {
console.log("I am studying " + this.subject);
}
}
let student = new Student("John", 30, "math");
student.greet(); // outputs "Hello, my name is John and I am 30 years old."
student.study(); // outputs "I am studying math"
[/dm_code_snippet]
In this example, we have created a Student class that inherits from the Person class. The Student class has its own constructor method that takes a subject parameter in addition to the name and age parameters. The constructor calls the parent class’s constructor using the super keyword. This sets up the name and age properties of the Student object. The Student class also has its own study() method, which prints out a message about the subject the student is studying.
Conclusion
In this tutorial, we have covered how to use JavaScript classes to create objects and deal with inheritance. Classes provide a much simpler and clearer syntax for creating objects and dealing with inheritance than the previous prototype-based inheritance model. We have also seen how to create objects from classes and how to use class inheritance to share methods and properties between classes.