【问题标题】:module.exports that include all functions in a single line在一行中包含所有功能的 module.exports
【发布时间】:2016-02-08 22:40:20
【问题描述】:

这是In Node.js, how do I "include" functions from my other files?的后续问题

我想包含一个包含 node.js 应用程序常用功能的外部 js 文件。

In Node.js, how do I "include" functions from my other files? 中的答案之一,这可以通过

// tools.js
// ========
module.exports = {
  foo: function () {
    // whatever
  },
  bar: function () {
    // whatever
  }
};

var zemba = function () {
}

每个函数都导出不方便。是否有可能有一个导出所有功能的单线?看起来像这样的东西;

module.exports = 'all functions';

这样方便多了。万一以后忘记导出某些功能,它的错误也更少。

如果不是单行,是否有更简单的替代方案可以使编码更方便?我只是想方便地包含一个由常用函数组成的外部js文件。类似于 C/C++ 中的 include <stdio.h>

【问题讨论】:

  • 没有。这不是 Javascript 的工作方式。你提到的方式是最有效的方式。

标签: javascript node.js import header


【解决方案1】:

您可以先编写所有函数声明,然后将它们导出到一个对象中:

function bar() {
   //bar
}

function foo() {
   //foo
}

module.exports = {
    foo, bar
};

虽然没有神奇的单线,但您需要明确导出您想要公开的功能。

【讨论】:

  • 谢谢。我想这是目前最好的 javascript 可以提供的。这是让我欣赏 python 的一件事。
【解决方案2】:

我做了以下类似的事情:

var Exported = {
   someFunction: function() { },
   anotherFunction: function() { },
}

module.exports = Exported;

我需要它在另一个文件中,我可以访问这些功能

var Export = require('path/to/Exported');
Export.someFunction();

这本质上只是一个包含函数的对象,然后你导出该对象。

【讨论】:

  • 这个方案是最省事的。
  • 但是,如果您希望其中一个函数调用另一个函数,则此解决方案将失败。你可以通过使用类来解决这个问题。
  • @mareoraft 它是如何失败的?要在 someFunction 中调用 anotherFunction 只需执行 this.anotherFunction()
【解决方案3】:

值得注意的是,在 ES6 中,您现在可以像这样导出函数:

export function foo(){}
export function bar(){}
function zemba(){}

只需在要导出的函数前写上export。更多信息here.

【讨论】:

    【解决方案4】:

    如果你使用 ES6,你可以这样做:

    function bar() {
       //bar
    }
    
    function foo() {
       //foo
    }
    
    export default { bar, foo };

    【讨论】:

      【解决方案5】:

      一个非常老的问题,但我只需要自己解决同样的问题。 我使用的解决方案是在模块内定义一个类来包含我的所有函数并简单地导出该类的一个实例。

      classes.js 看起来像这样:

      class TestClass
      {
         Function1() {
              return "Function1";
          } 
          Function2() {
              return "Function2";
          }
      }
      
      module.exports = new TestClass();
      

      app.js 看起来像这样:

      const TestClass = require("./classes");
      console.log( TestClass.Function1);
      

      只要继续向类中添加更多函数,它们就会被导出:)

      【讨论】:

        【解决方案6】:
        const fs = require("fs")
        
        
        var ExportAll = {
        
            deleteFile : function deleteFile(image,folder="uploads"){
            
                let imagePath = `public/${folder}/${image}`
            
                if (fs.existsSync(imagePath)){
                    fs.unlinkSync(imagePath)
                }
            },
        
        
            checkFile : function checkFile(image,folder="uploads"){
            
                let imagePath = `public/${folder}/${image}`
            
                if (fs.existsSync(imagePath)){
                    return true
                }
                else{
                    return false
                }
            },
            
        
            rand : function(min=1,max=10) 
                   {
                return Math.floor((Math.random() * max) + min)
                  }
        }
        
        module.exports = ExportAll
         
        

        【讨论】:

        • 请对您尝试过的方法和无效的方法进行说明,以使您的问题更加清晰
        【解决方案7】:

        从具有导出函数的类型模块文件中导入所有内容。

        在这里找到: https://javascript.info/import-export

        myfile.js

        export function myFunction()
        {
        // ................
        }
        
        export function myFunction2()
        {
        // ................
        }
        

        myfile2.js - 导入文件中导出的所有内容

        import * as myFunctions from './myfile.js';
        
        
        // Usage 
        
            myFunctions.myFunction(); 
            myFunctions.myFunction2();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-05-31
          • 2018-02-03
          • 2020-08-05
          • 1970-01-01
          • 2021-10-24
          • 1970-01-01
          • 1970-01-01
          • 2017-02-23
          相关资源
          最近更新 更多