Home  >  Article  >  Web Front-end  >  nodejs calls class method

nodejs calls class method

WBOY
WBOYOriginal
2023-05-23 12:56:38925browse

Node.js is a JavaScript runtime environment based on the Chrome V8 engine, which is lightweight, efficient, and cross-platform. In Node.js, we can call class methods by referencing the class for code reuse and better readability. This article will introduce how to call class methods in Node.js.

1. Create classes and class methods

In Node.js, you can create a class through the class keyword. A class is a template for an object that describes the object's properties and methods. Class methods can be shared by all instances of the class, thereby achieving code reuse. The following is an example of a simple class:

class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

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

The above code defines a Person class, which contains a constructor and an instance method sayHello(). The constructor is used to create a Person instance and initialize the instance's attributes name and age; the instance method sayHello() is used to print the instance's name and age attribute values.

2. Export class

In Node.js, if you need to use this class in other files, you need to export it through exports or module.exports. The following is a way to export the Person class Example:

// person.js
class Person {
  constructor(name, age) {
    this.name = name;
    this.age = age;
  }

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

module.exports = Person;

In the above example, the Person class is exported through module.exports, and other files can reference this class through the require function.

3. Import class

In other files, you can import the Person class through the require function. The following is an example of calling the Person class method:

// main.js
const Person = require('./person');
let person = new Person('Tom', 18);
person.sayHello();

In the above example, The person.js file is introduced through the require function, the Person class is obtained, a Person instance is created through the constructor, and finally the instance method sayHello() is called.

4. Calling class methods

Class methods can be called directly through the class name, or through instances of the class. The following are two examples of calling class methods:

// 调用类方法1
Person.sayHi();

// 调用类方法2
let person = new Person('Tom', 18);
person.sayHi();

In the above example, a class method named sayHi() is called. The first calling method is to call directly through the Person class name, and the second calling method is The way is to call it through a Person class instance.

5. Conclusion

In Node.js, you can define a class through the class keyword and export it through exports or module.exports. In other files, you can import classes through the require function and call class methods through the class name or class instance. The calling method of class methods is more readable and extensible, which helps code reuse and maintenance.

The above is the detailed content of nodejs calls class method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn