【问题标题】:can we have more than one module.exports statements in one js file我们可以在一个 js 文件中有多个 module.exports 语句吗
【发布时间】:2012-08-20 07:01:00
【问题描述】:

我是 javascript 的新手。我正在使用 Titanium studio 开发移动应用程序。 我的问题是关于javascript的,如下所示。

我在这个文件中有一个名为 commonUi.js 的文件 js 文件,我正在定义可以在不同窗口中使用的通用 UI 组件。

现在我在 commonUi.js 中定义了两个对象函数,如下所示。

function component1(){

}

function component2(){

}

现在我的核心问题是我可以在我的 commonUi.js 文件中编写以下两个语句

  1. module.exports = 组件1;
  2. module.exports = 组件2;

非常感谢任何帮助。

【问题讨论】:

    标签: javascript appcelerator titanium-mobile


    【解决方案1】:

    你的模块中只能有一个module.exports =,但你可以这样做来实现你想要的:

    var CommonUi = function() {
    
        var component1 = function() {
    
        }
    
        var component2 = function() {
    
        }
    
        return {
            component1: component1
           ,component2: component2
        }
    }();
    
    module.exports = CommonUi;
    

    然后你可以像这样使用它:commonUi.component1();

    【讨论】:

    • 你的意思是说我们不能在一个javascript文件中使用多个module.export语句。
    • @Android_Crazy 完全正确。 1 个 JS 文件是 1 个模块。你当然可以为 10 个模块使用 10 个文件。
    猜你喜欢
    • 2011-12-10
    • 1970-01-01
    • 2015-09-16
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多