通过关键字static来标记类的静态字段、静态方法,即 不需要通过类的实例,也能够访问它们。
类的内部,通过this关键字,来引用当前类的静态字段或静态方法。
# person.js
class Person {
    static userName = 'albert';
    static age = 18;
    static showAge() {
        console.log(this.showName());
        return this.age;
    }
    static showName() {
        return this.userName;
    }
}
module.exports = Person;
# test.js
const Person = require("./person");
console.log(Person.showAge())
输出:
albert 18
静态异步方法:
当一个方法既是static方法,又是异步await方法时,两者的顺序是:static async
const DatetimeHelper = require("../framework/helper/datetime_helper");
const RobotModel = require("../model/robot_model");
/**
 * 任务 服务类
 */
class TaskService {
    /**
     * 刷新心跳时间
     * @param curRobotId 当前机器人ID
     */
    static async refreshHeartbeatTime(curRobotId) {
        let update = {
            "heartbeat_time" : DatetimeHelper.now()
        };
        return await RobotModel.updateById(curRobotId, update);
    }
}
module.exports = TaskService;