【问题标题】:How do I call up another JavaScript file from one main file?如何从一个主文件调用另一个 JavaScript 文件?
【发布时间】:2018-07-05 03:40:09
【问题描述】:

我正在使用 discord.js 库为 Discord 创建一个机器人。我正在尝试创建一个文件,这样当我执行它时,它会加载每个模块,而我不必多次运行node file.js。我们称之为startup.js 的文件位于模块文件夹中。假设我要加载位于Bot/modules/diagnostics 中的File1.jsFile2.jsFile3.js,而startup.js 在Bot/modules 中。我希望文件异步加载,顺序不重要。

感谢所有帮助。

【问题讨论】:

  • "... 位于模块文件夹中,但不在任何特定文件夹中" 没有多大意义。你试过require()吗?
  • 不在任何特定文件夹中,我的意思是它不在诊断程序中。它只是在模块文件夹中。也可以,我试过 require()。
  • 你试过了,然后...?
  • 我可以打电话给config.json,但是,我还没有用JavaScript文件尝试过。我的错。我现在就试试。

标签: javascript node.js discord.js


【解决方案1】:

您可以将 File1、File2 和 File3 作为模块导入。 第一种方法:

    //File1.js:
    module.exports = {
        nameOfYourFunction: () => {
            //Your code here
        },
        otherFunction: (args) => {
            //Your code here
        }
    };
    //Do the same for File2, File3, and other files.

    //startup.js:
    const file1 = require("./diagnostics/File1.js"), file2 = require("./diagnostics/File2.js"), file3 = require("./diagnostics/File3.js");
    // Then use this to run your code from File1.js:
    file1.nameOfYourFunction();
    file1.otherFunction(args);
    //Do the same thing to run your code from other files

第二种方法:

    /* If you only have one function that you want to run,
    you can set the module's entire exports to a function. This method will
    not allow you to create multiple functions.*/

    //File1.js:
    module.exports = (args) => {
        //Your code here
    };
    //Do the same thing or the First method for other files.

    //startup.js:
    const file1 = require("./diagnostics/File1.js"), file2 = require("./diagnostics/File2.js"), file3 = require("./diagnostics/File3.js");
    //To run your code:
    file1(args);

编辑:使用这些方法时要小心,因为它们与 startup.js 中的主代码处于不同的范围内。

【讨论】:

    【解决方案2】:

    要给出更详细的答案,有几种方法可以做到这一点。第一种方法是将 module.exports 与对象一起使用。这将允许您使用require(或者,使用像 babel 或 webpack 这样的编译器,import)来导入模块,并且您可以调用对象中的任何函数。这是使用@MaxxiBoi 的响应方式。

    // File 1
    module.exports = {
       "myFunction1": (arg1, arg2) => {
           console.log("Function 1 with 2 args: "+ arg1 + " " + arg2);
       },
       "myFunction2": () => {
           console.log("Function 2");
       }
    }
    
    // File 2
    const myModule = require("./file1.js");
    myModule.myFunction1(null, "Hi"); // Logs "Function 1 with 2 args: null Hi"
    myModule.myFunction2(); // Logs "Function 2"
    

    虽然这在您想要输出多个函数的情况下很有用,但如果您只想要每个模块一个函数,我不会这样做。

    第二种方法是使用带有变量或函数的 module.exports,而不是对象。这可以减少混乱并使其更易于理解。

    // File 1
    module.exports = myFunction1(arg1, arg2) {
        console.log("Function 1 with 2 args: " + arg1 + " " + arg2);
    }
    
    // File 2
    const myFunction = require("./file1.js");
    myFunction(null, "Hi"); // Logs "Function 1 with 2 args: null Hi"
    

    最后,还有另一种使用 ES5 或 ES6(在本例中我使用 ES6)创建构造函数的方法,这将允许您向其中传递更多变量,然后您可以在该类中引用这些变量。在此示例中,我使用 Discord.js 客户端并从构造函数中获取客户端的名称。假设客户的名字是“George”。

    // File 1
    module.exports = class MyClass {
        constructor(client) {
            this.client = client;
        }
        myFunction1(myVar2) {
            console.log("Function 1 with 2 args: " + this.client.user.username + " " + myVar2);
        }
        myFunction2() {
            console.log("Function 2");
        }
    }
    
    // File 2
    const MyClass = require("./MyClass.js");
    const myClassInstance = new MyClass(client);
    myClassInstance.myFunction1("Hi"); // Logs "Function1 with 2 args: George Hi"
    myClassInstance.myFunction2(); // Logs "Function 2"
    

    最后,这完全取决于您喜欢什么以及您想怎么做。每种方法都有其优缺点。如果您想了解更多关于我如何制作所有这些以及模块的一般工作原理,请查看Node.js docs explanation。要了解有关类(第三个模块中使用的类)的更多信息,请查看MDN documentation。希望我能够帮助您并为您提供选择。您可能还想查看this StackOverflow question,因为它可以解决您关于在不同目录中引用文件的问题。编码愉快!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-04
      • 2021-11-20
      • 2016-01-07
      • 1970-01-01
      • 1970-01-01
      • 2019-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多