class class in JavaScript

what is class

A class in JavaScript is a template or blueprint for creating objects. They provide an easy and elegant way to organize code and make it reusable and maintainable. Using a class, you define the properties and methods of an object, so that the class can be instantiated in multiple places to create new objects.

A class is an object-oriented programming concept that is built into many object-oriented programming languages. Specifically, a class is a template for creating objects, initializing data, and defining functions, and a way to organize information about a certain class of objects in a reusable format. By using a class, we can create a specific instance of said class and access data and functions of a specific class.

For example, suppose you want to create cat, dog, and rabbit objects. You can create each object individually, duplicating nearly the same code for each new object. Or you could create a template called animals, and simply create cat, dog, and rabbit objects by passing special information to the animal template. Classes are the concept of using templates to create different but similar objects without rewriting the code.
 

class composition

Classes can contain constructor methods, instance methods, getters, setters, and static class methods, but none of these are required.
Empty class definitions are still valid. By default, code in a class definition is executed in strict mode.

Classes in JavaScript

With the update of ES6 in 2015, JavaScript also has the function of classes. Although it's not as fleshed out and detailed as other object-oriented languages, it's still there.

Classes in JavaScript classare declared with the keyword. Here is an example of creating a class:

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 the example above, we defined a Personclass named with two properties: nameand age, and a method sayHello(). A constructor ( constructor) is a method called when a class is instantiated to initialize an object and set its properties. In our example, the constructor takes two parameters nameand and ageassigns them respectively to properties of the class. sayHello()The method is a member function of the class that will output the properties of the object to the console.

To create a new object, we use newthe keyword, and call the constructor of the class, as follows:

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

This will create a johnnew object named with nameand ageproperties, and sayHello()a method. We can use dot syntax to access and modify properties of objects and to call methods on objects.

Classes in JavaScript can also inherit from other classes. Subclasses can override the methods of the parent class and add their own properties and methods. Here is an example:

class Student extends Person {
  constructor(name, age, grade) {
    super(name, age);
    this.grade = grade;
  }

  sayHello() {
    console.log(`Hello, my name is ${this.name}, I am ${this.age} years old, and I am in grade ${this.grade}.`);
  }
}

const jane = new Student('Jane', 12, 6);
jane.sayHello(); // 输出 "Hello, my name is Jane, I am 12 years old, and I am in grade 6."

In the example above, we defined a Studentsubclass called that inherits from Personthe class. Subclasses override sayHello()the method and add a new property grade. We use superthe keyword to call the superclass's constructor, passing it namethe and age. We also created a janenew object called and called its sayHello()method.

In conclusion, classes are a powerful tool that can help you write cleaner, more modular code. They make code easy to understand and maintain, and provide a simple and elegant way to create objects.

Guess you like

Origin blog.csdn.net/qq_44848480/article/details/131130218