AMD 格式是否要求模块是单例的?
Does AMD format require that modules are singletons?
AMD 规范中是否有任何内容规定要求的模块必须与同一对象一起提供?假设一个 require'd 模块是提供给所有需要的模块的单个实例似乎是相当普遍的做法,但是有什么可以阻止模块加载器将加载的模块视为仅仅缓存(可能在某个时候重新加载它们)?
例如(假设地说),AMD 加载程序是否可以保证在各种不同的依赖模块之间分发消息总线模块的相同实例,以便它们可以使用它来相互发送消息?
是的,模块应该是单例的。
来自spec:
define() function
...
factory
The third argument, factory, is a function that should be executed to instantiate the module or an object. If the factory is a function it should only be executed once. If the factory argument is an object, that object should be assigned as the exported value of the module.
AMD 规范中是否有任何内容规定要求的模块必须与同一对象一起提供?假设一个 require'd 模块是提供给所有需要的模块的单个实例似乎是相当普遍的做法,但是有什么可以阻止模块加载器将加载的模块视为仅仅缓存(可能在某个时候重新加载它们)?
例如(假设地说),AMD 加载程序是否可以保证在各种不同的依赖模块之间分发消息总线模块的相同实例,以便它们可以使用它来相互发送消息?
是的,模块应该是单例的。
来自spec:
define() function
...
factory
The third argument, factory, is a function that should be executed to instantiate the module or an object. If the factory is a function it should only be executed once. If the factory argument is an object, that object should be assigned as the exported value of the module.