【发布时间】:2011-06-19 15:56:51
【问题描述】:
我有许多 JavaScript “类”,每个类都在自己的 JavaScript 文件中实现。对于开发,这些文件是单独加载的,对于生产它们是连接的,但是在这两种情况下,我都必须手动定义加载顺序,确保如果 B 使用 A,则 B 在 A 之后。我打算使用 RequireJS 作为实现CommonJS Modules/AsynchronousDefinition 自动为我解决这个问题。
有没有比定义每个导出一个类的模块更好的方法呢?如果不是,您如何命名模块导出的内容?如下例所示,导出类“Employee”的模块“employee”对我来说感觉不够 DRY。
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");
});
【问题讨论】:
标签: javascript commonjs requirejs