简体   繁体   English

node.export中的module.exports = function的含义

[英]meaning of module.exports= function in node.js

I am taking first steps with node.js and obviously one of the first things i tried to do was exporting some data from a module, so i tried this simple case: 我正在使用node.js迈出第一步,显然我尝试做的第一件事就是从模块中导出一些数据,所以我尝试了这个简单的例子:

dummy.js: dummy.js:

var user = "rally";

module.exports = {
  user:user
};

and than required it from a different file like this: 而不是从这样的不同文件中要求它:

var dummy = require('./dummy.js');
console.log(dummy.user); // rally

So far so good, every thing works, but now i dived into code where there is this definition in the beginning of the module: 到目前为止一切顺利,每件事都有效,但现在我潜入了代码,在模块的开头有这个定义:

module.exports = function(passport,config, mongoose) {}

and i don't understand whats the meaning of it and how can i work with it. 我不明白它的含义是什么,我怎么能用它。 just for trying to understand i defined some variables inside this abstract function but couldn't get their value from any other file. 只是为了试图理解我在这个抽象函数中定义了一些变量但是无法从任何其他文件中获取它们的值。 any idea how can i export variables from module defined like this..? 任何想法如何从像这样定义的模块中导出变量..? so for example i could require this module and get the "Dummy" variable and use it in a different file 所以例如我可以要求这个模块并获取“Dummy”变量并在不同的文件中使用它

module.exports = function(passport,config, mongoose) {
var dummy = "Dummy";
}

It works exactly the same as the first one does, only that it exports a function instead of an object. 它与第一个完全相同,只是它导出一个函数而不是一个对象。

The module that imports the module can then call that function: 然后,导入模块的模块可以调用该函数:

var dummy = require('./dummy.js');
dummy();

any idea how can i export variables from module defined like this..? 任何想法如何从像这样定义的模块中导出变量..?

Since functions are just objects, you can also assign properties to it: 由于函数只是对象,您还可以为其指定属性:

module.exports = function(passport,config, mongoose) {}
module.exports.user = 'rally';

However I'd argue that this is less expected if a module directly exports a function. 但是我认为,如果模块直接导出函数,则预期会减少。 You are probably better off exporting the function as its own export: 您可能最好将该函数导出为自己的导出:

exports.login = function(passport,config, mongoose) {}
exports.user = 'rally';

WHAT IS A MODULE? 什么是模块?

A module encapsulates related code into a single unit of code. 模块将相关代码封装到单个代码单元中。 When creating a module, this can be interpreted as moving all related functions into a file. 创建模块时,可以将其解释为将所有相关功能移动到文件中。

// dummy.js
var exports = module.exports = {};

The utility of dummy.js increases when its encapsulated code can be utilized in other files. 当其封装的代码可以在其他文件中使用时, dummy.js的实用程序dummy.js增加。 This is achieved by using exports . 这是通过使用exports来实现的。

HOW ARE THEY INVOKED? 他们如何被调查?

You could declare your functions outside of the module.exports block. 您可以在module.exports块之外声明您的函数。 Functions inside exports can be invoked exactly the same way as variables or any other object. 导出中的函数可以与变量或任何其他对象完全相同的方式调用。

EXAMPLE

//dummy.js

var myVariable = "foo";
var myFunction = function(){
     //some logic
};

module.exports{

      myVariable : myVariable,
      myFunction : myFunction,

      myVariableTypeTwo : "bar",
      myFunctionTypeTwo : function () {
           //some logic
      }

}

We can now access the publicly available methods of dummy.js as a property from any js file. 我们现在可以从任何js文件中访问dummy.js的公共可用方法作为属性。

var dummy = require('./dummy.js');

dummy.myVariable; //foo
dummy.myFunction();

dummy.myVariableTypeTwo; //bar
dummy.myFunctionTypeTwo();

NOTE 注意

In the code above, we could have replaced module.exports with exports and achieved the same result. 在上面的代码中,我们可以使用exports替换module.exports并获得相同的结果。 If this seems confusing, remember that exports and module.exports reference the same object. 如果这看起来令人困惑,请记住exports和module.exports引用相同的对象。

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

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