我有许多 JavaScript“类”,每个类都在自己的 JavaScript 文件中实现。对于开发,这些文件是单独加载的,对于生产它们是串联的,但在这两种情况下,我都必须手动定义加载顺序,确保如果 B 使用 A,则 B 在 A 之后。我计划使用RequireJS作为CommonJS Modules/AsynchronousDefinition自动为我解决这个问题。
有没有比定义每个导出一个类的module更好的方法呢?如果没有,您如何命名module导出的内容?如下例所示,导出类“Employee”的module“employee”对我来说感觉不够干燥。
define("employee", ["exports"], function(exports) {
exports.Employee = function(first, last) {
this.first = first;
this.last = last;
};
});
define("main", ["employee"], function (employee) {
var john = new employee.Employee("John", "Smith");
});