Home >Web Front-end >Front-end Q&A >Detailed introduction to the method of adding classes in JavaScript
JavaScript is a programming language widely used in Internet development. It can help web pages achieve dynamic effects and interactivity. In JavaScript, a class is an object that encapsulates data and methods. By adding classes, we can better organize the code and improve development efficiency. The following will introduce in detail the method of adding classes in JavaScript.
1. Define a class
In JavaScript, defining a class requires using the class keyword and class name. As shown below, we defined a class named Person:
class Person { constructor(name, age) { this.name = name; this.age = age; } greetings() { console.log("Hello, my name is " + this.name + " and I am " + this.age + " years old."); } }
In the above code, we added two attributes named name and age to the Person class through the constructor method, and added them through the greetings method. Say hello function.
2. Create objects
To create a class object, you need to use the new keyword and class name. As shown below, we created a Person class object named person:
let person = new Person("Tom", 20);
Through the above code, we successfully created a Person class object named Tom with an age of 20.
3. Inherited classes
In JavaScript, classes can be inherited, and subclasses can inherit all properties and methods of the parent class. Next we create a subclass named Student, which inherits from the Person class:
class Student extends Person { constructor(name, age, school) { super(name, age); this.school = school; } study() { console.log(this.name + " is studying at " + this.school); } }
In the above code, we specify that the Student class inherits from the Person class through the extends keyword. In the constructor method, we get the attributes of the parent class by calling the super method and add the school attribute. In the study method, we added the learning function.
4. Use super class methods
In the inheritance relationship, subclasses can also use the methods of the parent class. For example, we call the greetings() method of the parent class in the Student class:
class Student extends Person { constructor(name, age, school) { super(name, age); this.school = school; } study() { console.log(this.name + " is studying at " + this.school); } greetings() { super.greetings(); console.log("I am a student."); } }
In the above code, we obtain the greetings() method of the parent class through the super keyword and add our own in the subclass Say hello message.
Summary
Through the above introduction, we have learned how to define, create, inherit and use classes in JavaScript. Developers can flexibly use these technologies according to their own needs to improve code readability and efficiency. In actual work, we can also combine these technologies to complete more complex project development.
The above is the detailed content of Detailed introduction to the method of adding classes in JavaScript. For more information, please follow other related articles on the PHP Chinese website!