简体   繁体   English

在RequireJS中定义模块时,返回对象与返回init()函数之间有什么区别?

[英]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? 当在另一个模块中需要它时,以下两个实现之间有什么区别?

1. 1。

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;
    ...
});

2. 2。

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.

相关问题 在Durandal视图模型中返回函数和返回对象有什么区别? - What is the difference between returning a function and returning an object in a Durandal viewmodel? JS Module: Difference between directly returning a function in an object and returning a function in an object returning a function - JS Module : Difference between directly returning a function in an object and returning a function in an object returning a function 将IIFE的公共成员分配给变量与返回对象之间有什么区别 - What is the difference between assigning an IIFE's public members to a variable vs returning an object 构造函数 function 和 function 返回 object 之间有什么区别? - What is the difference between a constructor function and function returning an object? React:返回 JSX 的 function 和 function 组件有什么区别? - React: what's the difference between a function returning JSX and function component? 兼容AMD的JavaScript-定义然后返回与仅返回之间的任何区别 - AMD-compliant JavaScript - any difference between defining then returning vs just returning RequireJS从模块返回要在模板中使用的函数 - RequireJS returning a function from module to be used in template 在递归期间返回 function 调用与仅调用 function 有什么区别? - What is the difference between returning a function call vs only calling the function again during recursion? 在为QScriptEngine重新定义“print()”函数时返回“未定义值”有什么意义? - What's the point of returning an “Undefined Value” when re-defining “print()” function for QScriptEngine? 在函数内返回函数vs语句之间的区别 - The difference between returning a function vs statement inside a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM