使用预定义exports
对象。以下适用于.coffee
和.js
文件:
把下面给schemas.coffee
:
exports.EntrySchema = new Schema
aa : String
record : String
status : String
exports.CreditSchema = new Schema
credit : Number
minute : Number
date : Date
#belongs_to subdomain
然后在main.js你可以这样做:
var s = require('./schemas')
var foo = s.CreditSchema
S成为模块的exports
对象编号为./schemas
。 ./schemas.coffee
也可以工作,但不建议这样做,因为后来您可以重构schemas.coffee
至schemas.js
或 schemas/
子文件夹,甚至是以C++实现的DLL。
通常的做法是使用的模块名称相同的名称和参考文献出口对象:
var schemas = require('./schemas')
另一种常见的做法是,既foo
和exports.foo
每一个出口:
EntrySchema = exports.EntrySchema = new Schema
aa : String
record : String
status : String
CreditSchema = exports.CreditSchema = new Schema
credit : Number
minute : Number
date : Date
#belongs_to subdomain
因此,您仍然可以在schemas.coffee
内使用较短的EntrySchema
名称,并且不必在schemas.coffee
以内的任何地方使用exports.EntrySchema
。
这两种做法都很方便,但既不要求也不强制。
这个exports
设施是CommonJS模块规范的实现。见http://www.commonjs.org/specs/modules/1.0/为标准,并http://nodejs.org/docs/latest/api/modules.html在node.js的具体实施
它正常工作,直到有一个像这样的嵌入文档:\t exports.AccountSchema =新架构 \t电话:字符串 \t用户:[UserSchema] \t custrs:[CustphoneSchema] \t credit:Number – donald
它返回:ReferenceError:UserSchema未定义 – donald
修改答案。另一种解决方案是仅导出顶层模式。 – nponeccop