在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