require()实际返回什么,文件或函数 [英] What does require() actually return, the file or the function

查看:1190
本文介绍了require()实际返回什么,文件或函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有profile.js

For example, I have 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中,我有

In my app.js, I have

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(用户名)怎么办,我应该如何导出这两个函数并在app.js中使用它们?

So the variable is the profile.js itself or the function Profile(username)? What if the profile.js have different functions, say I have function SetProfile(username) in the profile.js, how should I export those two functions and use them in the app.js?

推荐答案

require(...)函数返回 module.exports 来自required模块的值,如果是 Profile 函数。

The require(...) function returns the module.exports value from the "required" module, and in the case its the Profile function.

< hr />
顺便说一句,我不知道返回文件配置文件是profile.js本身是什么意思。

这篇关于require()实际返回什么,文件或函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆