在Node.js的JavaScript ES6中定义静态类私有属性的最佳实践方式是什么?在JavaScript中定义静态类属性
我有以下Log
类:
'use strict';
const moment = require('moment');
const LOG_DATE_FORMAT = 'YYYY-MM-DDTHH:mm:ss.SSSZ';
module.exports = class {
static debug(txt) {
console.log(`${moment().utc().format(LOG_DATE_FORMAT)} [debug] ${txt}`);
}
static info(txt) {
console.info(`${moment().utc().format(LOG_DATE_FORMAT)} [info] ${txt}`);
}
static warn(txt) {
console.warn(`${moment().utc().format(LOG_DATE_FORMAT)} [warn] ${txt}`);
}
static error(txt) {
console.error(`${moment().utc().format(LOG_DATE_FORMAT)} [error] ${txt}`);
}
};
是定义moment
和DATE_FORMAT
外module.exports
的,因此类定义这样做的正确方法是什么?
是的,这是一种可以接受的做事方式。你在多个函数中共享一些变量,所以你必须在更高的范围内定义它们,以便所有的函数都可以访问它们。 – jfriend00
为什么不导出变量呢? – wrangler