如何从一个主文件调用另一个 JavaScript 文件?

How do I call up another JavaScript file from one main file?

我正在使用 discord.js 库为 Discord 创建一个机器人。我正在尝试创建一个文件,以便在我执行它时加载每个模块,而无需多次 运行 node file.js。我们将调用 startup.js 的文件位于模块文件夹中。假设我想加载 File1.jsFile2.jsBot/modules/diagnostics 中的 File3.js,而 Bot/modules 中的 startup.js。我希望文件异步加载,顺序不重要

感谢所有帮助。

您可以将 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 中的主要代码处于不同的范围内。

要给出更详细的答案,有几种方法可以做到这一点。执行此操作的第一种方法是对对象使用 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),这将允许您将更多变量传递给它,然后您可以在上述内容中引用class。在此示例中,我使用 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. To learn more about classes (the one used in the third module), check out the MDN documentation. Hope I was able to help and give you options. You may also want to take a look at this Whosebug question,因为它将解决您关于在不同目录中引用文件的问题。编码愉快!