简体   繁体   English

NodeJs:如何导出Javascript文件中的所有模块和变量?

[英]NodeJs: How to export all the modules and variables in a Javascript file?

How can I export all the modules and variables present in a Javascript file using NodeJS. 如何使用NodeJS导出Javascript文件中存在的所有模块和变量。 Suppose i have a .js file with 20 functions and 20 variables and i need to export all of the functions and variables at once and not one by one. 假设我有一个包含20个函数和20个变量的.js文件,并且我需要一次而不是一个一个地导出所有函数和变量。 How can i achieve this? 我怎样才能做到这一点?

You can't. 你不能 You must explicitly name them, in some way. 您必须以某种方式明确命名它们。

If Amberlamps' answer is not possible (you want them at the base level), you could still do: 如果无法获得Amberlamps的答案(您希望它们在基本级别上),您仍然可以执行以下操作:

module.exports = {
  functionA: function () {
    // TODO
  },
  functionB: function () {
    // TODO
  },
  // the rest
};

How about this: 这个怎么样:

exports.MyApp = {
    functionA: function() {
        // TODO
    },
    functionB: function() {
        // TODO
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM