gpt4 book ai didi

javascript - require() 实际返回什么,文件或函数

转载 作者:行者123 更新时间:2023-11-29 16:46:39 26 4
gpt4 key购买 nike

比如我有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(username)?如果 profile.js 有不同的函数怎么办,比如我在 profile.js 中有函数 SetProfile(username),我应该如何导出这两个函数并在 app.js 中使用它们?

最佳答案

require(...) 函数从“required”模块返回 module.exports 值,在本例中是 Profile函数。


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

关于javascript - require() 实际返回什么,文件或函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40270731/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com