[英]When defining module in RequireJS, what's the difference between returning an object VS returning an init() function?
I have a JS module to store some configuration objects. 我有一个JS模块来存储一些配置对象。 I'm using it like enums in c#.
我像在C#中的枚举一样使用它。
When requiring it in another module, what's the difference between the following 2 implementations? 当在另一个模块中需要它时,以下两个实现之间有什么区别?
Types.js Types.js
define([], function(){
var types = {
typeA: "Type A",
typeB: "Type B",
typeC: "Type C"
};
return {types: types};
});
Usage 用法
define(["Types.js"], function(types){
var types = types;
...
});
Types.js Types.js
define([], function(){
var init = function(){
var types = {
typeA: "Type A",
typeB: "Type B",
typeC: "Type C"
};
return {types: types};
};
return {init: init};
});
Usage 用法
define(["Types.js"], function(types){
var types = types.init();
...
});
Is there any differences between those 2 implementations? 这两种实现之间有什么区别吗? Or are they just the same?
还是一样?
The second implementation is more like OOP-style, because you can make several instances of the class Types: types = new Types(someParams); 第二种实现更像OOP风格,因为您可以创建Types类的多个实例:types = new Types(someParams); and use it fields and methods.
并使用它的字段和方法。 But the first implementation only returns object.
但是第一个实现只返回对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.