class 导出返回空对象

class export returning empty object

我在使用 module.export 导出时遇到错误 class。

当我使用 const Errors = require('errors.js'); 在另一个文件中要求 class 然后尝试使用 throw Errors.NotImplimented 我在 [=16= 的开头收到 undefined 错误].

如果我在要求它后尝试 console.log 错误 class,我会看到一个空对象。

'use strict';

class Errors {
    NotImplimented() {
        return new Error('Not implimented');
    }
    HTTP_500() {
        return new Error('500 Internal Server Error');
    }
    HTTP_404() {
        return new Error('404 Page Not Found');
    }
}

module.export = Errors;

两个问题。第一,它不出口。尝试 module.exports = Errors;

第二,您没有创建 class 的实例。尝试 const errors = new Errors();

此外(不是这个问题的情况),由于循环要求,您也可能得到一个空对象,more here