require()实际上返回的是文件还是函数

What does require() actually return, the file or the function

本文关键字:文件 函数 返回 实际上 require      更新时间:2023-09-26

例如,我有profile.js

var EventEmitter = require("events").EventEmitter;
var https = require("https");
var http = require("http");
var util = require("util");
    function Profile(username) {
     // function code here
    }
    util.inherits( Profile, EventEmitter );
    module.exports = Profile;

在app.js中,我有

var Profile = require("./profile.js");

var studentProfile = new Profile("chalkers");
/**
* When the JSON body is fully recieved the 
* the "end" event is triggered and the full body
* is given to the handler or callback
**/
studentProfile.on("end", console.dir);
/**
* If a parsing, network or HTTP error occurs an
* error object is passed in to the handler or callback
**/
studentProfile.on("error", console.error);

所以变量是Profile .js本身还是函数Profile(用户名)?如果profile.js有不同的函数,假设我在profile.js中有SetProfile(username)函数,我该如何导出这两个函数并在app.js中使用它们?

require(...)函数从"required"模块返回module.exports值,在这种情况下是Profile函数。


顺便说一句,我不知道"返回文件"或" Profile是Profile .js本身"是什么意思。