简体   繁体   English

Node.js模块导出混乱

[英]Nodejs module exports confusion

I have a confusion with module.exports. 我对module.exports感到困惑。 As far as I understood module.exports is an object that is exposed to other modules, 据我了解, module.exports是暴露给其他模块的对象,

exports=module.exports={} 

But in the index.js of morgan package in node.js I found this. 但是在node.js的morgan包的index.js中,我发现了这一点。

module.exports = morgan
module.exports.compile = compile
module.exports.format = format
module.exports.token = token

morgan , compile , format and token are all functions. morgancompileformattoken都是函数。

Can you explain whats happening here? 您能解释一下这里发生了什么吗? How is a function(morgan) assigned to module.exports ? 一个函数(摩根)如何分配给module.exports? After the first line is executed, is module.exports a function instead of an JSON object? 在执行第一行之后,module.exports是函数而不是JSON对象吗?

A Node module basically works like this: Node模块基本上是这样的:

var module = {
    exports: {}
};
(function (exports, require, module, __filename, __dirname) {

    // your module code here

})(module.exports, require, module, __filename, __dirname);

var exported = module.exports;

By default, exports , and module.exports both point to the same object. 默认情况下, exportsmodule.exports都指向同一个对象。 You can add properties to the object as normal. 您可以像往常一样向对象添加属性。 However, what if you want to return a function or some other object instead of just the default standard object? 但是,如果要返回函数或其他对象而不是默认的标准对象怎么办?

In that case, you can set module.exports to something else, and that will be the new exported object. 在这种情况下,您可以将module.exports设置为其他内容,这将是新的导出对象。

module.exports = function() {};

And of course, that function can have properties too, so your code is kind-of like this: 当然,该函数也可以具有属性,因此您的代码有点像这样:

module.exports = function(){};
module.exports.compile = function() {};
module.exports.format = function() {};
module.exports.token = function() {};

Which would be equivalent to: 相当于:

var morgan = function() {};
var compile = function() {};
var format = function() {};
var token = function() {};

morgan.compile = compile;
morgan.format = format;
morgan.token = token;
module.exports = morgan;

How is a function(morgan) assigned to module.exports ? 一个函数(摩根)如何分配给module.exports? After the first line is executed, is module.exports a function instead of an JSON object? 在执行第一行之后,module.exports是函数而不是JSON对象吗?

Yes, module.exports will be a function, in place of the default object (however there is no JSON here, JSON is not a JavaScript object, but an encoding format). 是的, module.exports将是一个函数,代替默认对象(但是这里没有JSON,JSON不是JavaScript对象,而是一种编码格式)。

You might want to read this answer for a more in-depth explanation: 您可能需要阅读以下答案以获得更深入的说明:

What is the purpose of node js module exports and how do you use it? 节点js模块导出的目的是什么?如何使用它?

Morgan is simply adding parameters to the module being exported (in this case compile , format , and token . When you require the module in your application using something like morgan = require('morgan') , you can then call morgan.format to return the format that was appended to the object. Morgan只是向正在导出的模块中添加参数(在这种情况下为compileformattoken 。当您使用morgan = require('morgan')类的东西在应用程序中需要该模块时,您可以调用morgan.format返回附加到对象的格式。

Hope that clears things up a bit! 希望能把事情弄清楚!

module.exports is an object that is exposed to other modules and files. module.exports是公开给其他模块和文件的对象。 This allows a convenient way to export variables, functions and more javascript features. 这提供了导出变量,函数和更多javascript功能的便捷方法。

As you can see here , when you initialize any variable to {} you are initializing an object. 如您在此处看到的,将任何变量初始化为{}时,您正在初始化一个对象。

An object can contain variables and functions, this is one of the things that makes Javascript really cool. 一个对象可以包含变量和函数,这是使Javascript真正酷的一件事。 You can easily pass complex objects with functions as parameters and do real nice clean code. 您可以轻松地以函数为参数传递复杂的对象,并编写出真正漂亮的干净代码。

So, short story, think about module.exports as an object that is exposed to other modules and files with variables and functions. 因此,简而言之,将module.exports视为一个对象,该对象暴露给具有变量和功能的其他模块和文件。 Just like passing an Object Oriented object as a parameter in Java or Ruby. 就像在Java或Ruby中将面向对象的对象作为参数传递一样。

Here is easily explained: Read More 这里很容易解释: 阅读更多

Can you explain whats happening here? 您能解释一下这里发生了什么吗?

An assignment statement is happening. 分配声明正在发生。

// path-to-my-module.js
module.exports = morgan
module.exports.compile = compile
module.exports.format = format
module.exports.token = token

For each of these 4 statements, there is an assignment statement. 对于这4条语句中的每条语句,都有一个赋值语句。 Looking at the first line, module.exports = morgan , that would mean the module.exports object is going to have a property called morgan with its value being the value of the identifier morgan 看第一行, module.exports = morgan ,这意味着module.exports 对象将具有一个名为morgan的属性,其值是标识符morgan的值

So when you use it like this: 因此,当您像这样使用它时:

var m = require('./path-to-my-module.js');
console.log(m.morgan);
console.log(m);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM