在我的系统中,我在浏览器中加载了许多"类",每个文件在开发过程中都是单独的文件,并连接在一起进行生产.在加载它们时,它们会在全局对象上初始化一个属性G,如下例所示:
var G = {};
G.Employee = function(name) {
this.name = name;
this.company = new G.Company(name + "'s own company");
};
G.Company = function(name) {
this.name = name;
this.employees = [];
};
G.Company.prototype.addEmployee = function(name) {
var employee = new G.Employee(name);
this.employees.push(employee);
employee.company = this;
};
var john = new G.Employee("John");
var bigCorp = new G.Company("Big Corp");
bigCorp.addEmployee("Mary");
Run Code Online (Sandbox Code Playgroud)
我没有使用自己的全局对象,而是根据James Burke的建议,考虑让每个类都有自己的AMD模块:
define("Employee", ["Company"], function(Company) {
return function (name) {
this.name = name;
this.company = new …Run Code Online (Sandbox Code Playgroud) 1.3.0 - 2012年1月11日从Underscore中移除了AMD(RequireJS)支持.如果您想将Underscore与RequireJS一起使用,您可以将其作为普通脚本加载,包装或修补您的副本,或下载分叉版本.
他们为什么要这样做?有人知道吗?因为他们仅在几个月前(10月)添加了它,并且据说 AMD(异步模块定义)远远优于CommonJS模块.
更新:截至2013年12月,再次受到支持.