Javascript Nodejs模块导出混乱
我对module.exports感到困惑。据我所知,module.exports是一个暴露于其他模块的对象Javascript Nodejs模块导出混乱,javascript,node.js,node-modules,Javascript,Node.js,Node Modules,我对module.exports感到困惑。据我所知,module.exports是一个暴露于其他模块的对象 exports=module.exports={} 但是在node.js中morgan包的index.js中我发现了这个 module.exports = morgan module.exports.compile = compile module.exports.format = format module.exports.token = token morgan、compile、
exports=module.exports={}
但是在node.js中morgan包的index.js中我发现了这个
module.exports = morgan
module.exports.compile = compile
module.exports.format = format
module.exports.token = token
morgan、compile、format和token都是函数
你能解释一下这里发生了什么事吗?如何将函数分配给module.exports?执行第一行后,module.exports是函数而不是JSON对象吗?module.exports是向其他模块和文件公开的对象。这提供了一种导出变量、函数和更多javascript特性的方便方法 如您所见,当您将任何变量初始化为{}时,您正在初始化一个对象 一个对象可以包含变量和函数,这是Javascript非常酷的原因之一。您可以轻松地将函数作为参数传递复杂对象,并编写真正漂亮的干净代码 所以,简而言之,把module.exports看作是一个对象,它公开给具有变量和函数的其他模块和文件。就像在Java或Ruby中将面向对象对象作为参数传递一样
这里很容易解释:module.exports是向其他模块和文件公开的对象。这提供了一种导出变量、函数和更多javascript特性的方便方法 如您所见,当您将任何变量初始化为{}时,您正在初始化一个对象 一个对象可以包含变量和函数,这是Javascript非常酷的原因之一。您可以轻松地将函数作为参数传递复杂对象,并编写真正漂亮的干净代码 所以,简而言之,把module.exports看作是一个对象,它公开给具有变量和函数的其他模块和文件。就像在Java或Ruby中将面向对象对象作为参数传递一样
这里很容易解释:您可能需要阅读此答案以获得更深入的解释: Morgan只是简单地将参数添加到正在导出的模块中(在本例中为compile、format和token)。当您在应用程序中使用morgan=require'morgan'之类的命令需要模块时,您可以调用morgan.format来返回附加到对象的格式
希望能把事情弄清楚一点 您可能想阅读此答案以获得更深入的解释: Morgan只是简单地将参数添加到正在导出的模块中(在本例中为compile、format和token)。当您在应用程序中使用morgan=require'morgan'之类的命令需要模块时,您可以调用morgan.format来返回附加到对象的格式 希望能把事情弄清楚一点 你能解释一下这里发生了什么事吗 正在发生赋值语句
// path-to-my-module.js
module.exports = morgan
module.exports.compile = compile
module.exports.format = format
module.exports.token = token
对于这4条语句中的每一条,都有一条赋值语句。查看第一行module.exports=morgan,这意味着module.exports对象将有一个名为morgan的属性,其值是标识符morgan的值
所以当你这样使用它时:
var m = require('./path-to-my-module.js');
console.log(m.morgan);
console.log(m);
var module = {
exports: {}
};
(function (exports, require, module, __filename, __dirname) {
// your module code here
})(module.exports, require, module, __filename, __dirname);
var exported = module.exports;
module.exports = function(){};
module.exports.compile = function() {};
module.exports.format = function() {};
module.exports.token = function() {};
你能解释一下这里发生了什么事吗
正在发生赋值语句
// path-to-my-module.js
module.exports = morgan
module.exports.compile = compile
module.exports.format = format
module.exports.token = token
对于这4条语句中的每一条,都有一条赋值语句。查看第一行module.exports=morgan,这意味着module.exports对象将有一个名为morgan的属性,其值是标识符morgan的值
所以当你这样使用它时:
var m = require('./path-to-my-module.js');
console.log(m.morgan);
console.log(m);
var module = {
exports: {}
};
(function (exports, require, module, __filename, __dirname) {
// your module code here
})(module.exports, require, module, __filename, __dirname);
var exported = module.exports;
module.exports = function(){};
module.exports.compile = function() {};
module.exports.format = function() {};
module.exports.token = function() {};
节点模块基本上是这样工作的:
var m = require('./path-to-my-module.js');
console.log(m.morgan);
console.log(m);
var module = {
exports: {}
};
(function (exports, require, module, __filename, __dirname) {
// your module code here
})(module.exports, require, module, __filename, __dirname);
var exported = module.exports;
module.exports = function(){};
module.exports.compile = function() {};
module.exports.format = function() {};
module.exports.token = function() {};
默认情况下,exports和module.exports都指向同一对象。可以将特性作为常规添加到对象。但是,如果要返回函数或其他对象,而不仅仅是默认的标准对象,该怎么办
在这种情况下,您可以将module.exports设置为其他对象,这将是新导出的对象
module.exports = function() {};
当然,这个函数也可以有属性,所以你的代码是这样的:
var m = require('./path-to-my-module.js');
console.log(m.morgan);
console.log(m);
var module = {
exports: {}
};
(function (exports, require, module, __filename, __dirname) {
// your module code here
})(module.exports, require, module, __filename, __dirname);
var exported = module.exports;
module.exports = function(){};
module.exports.compile = function() {};
module.exports.format = function() {};
module.exports.token = function() {};
这相当于:
var morgan = function() {};
var compile = function() {};
var format = function() {};
var token = function() {};
morgan.compile = compile;
morgan.format = format;
morgan.token = token;
module.exports = morgan;
如何将函数分配给module.exports?执行第一行后,module.exe是否导出函数而不是JSON对象
是的,module.exports将是一个函数,代替默认对象,但是这里没有JSON,JSON不是JavaScript对象,而是一种编码格式。节点模块基本上是这样工作的:
var m = require('./path-to-my-module.js');
console.log(m.morgan);
console.log(m);
var module = {
exports: {}
};
(function (exports, require, module, __filename, __dirname) {
// your module code here
})(module.exports, require, module, __filename, __dirname);
var exported = module.exports;
module.exports = function(){};
module.exports.compile = function() {};
module.exports.format = function() {};
module.exports.token = function() {};
默认情况下,exports和module.exports都指向同一对象。可以将特性作为常规添加到对象。但是,如果要返回函数或其他对象,而不仅仅是默认的标准对象,该怎么办
在这种情况下,您可以将module.exports设置为其他对象,这将是新导出的对象
module.exports = function() {};
当然,这个函数也可以有属性,所以你的代码是这样的:
var m = require('./path-to-my-module.js');
console.log(m.morgan);
console.log(m);
var module = {
exports: {}
};
(function (exports, require, module, __filename, __dirname) {
// your module code here
})(module.exports, require, module, __filename, __dirname);
var exported = module.exports;
module.exports = function(){};
module.exports.compile = function() {};
module.exports.format = function() {};
module.exports.token = function() {};
这相当于:
var morgan = function() {};
var compile = function() {};
var format = function() {};
var token = function() {};
morgan.compile = compile;
morgan.format = format;
morgan.token = token;
module.exports = morgan;
如何将函数分配给module.exports?在执行第一行之后,
module.exports是函数而不是JSON对象吗
是的,module.exports将是一个函数,代替默认对象,但是这里没有JSON,JSON不是JavaScript对象,而是一种编码格式。我了解到Nodejs模块导出混淆。我读到Nodejs模块导出混乱。在这四行执行之后,你能告诉我module.exports是什么样子吗?在这四行执行之后,你能告诉我module.exports是什么样子吗?在这四行执行之后,你能告诉我module.exports是什么样子吗?@MohammadYusufGhazi它将是一个函数对象,有3个函数对象属性。那么format、compile和token将成为module.exports的函数变量,这是一个函数?@MohammadYusufGhazi是的,当您需要时,它们将作为属性提供。这看起来像{[function:morgan]compile:[function:compile],format:[function:format],token:[功能:令牌]}执行完这4行后,你能告诉我module.exports是什么样子吗?@MohammadYusufGhazi它将是一个函数对象,具有3个函数对象属性。因此,format、compile和token将成为module.exports的函数变量,这是一个函数?@MohammadYusufGhazi是的,当你需要时,它们将作为属性提供这就是{[Function:morgan]compile:[Function:compile],format:[Function:format],token:[Function:token]}