双括号是什么意思 [英] What does double parentheses mean in a require
问题描述
var express = require('express');
var app = express();
但是有时候,我在需求之后看到两个括号。
var routes = require('./ routes')(app);
Q)这是什么意思,它是如何工作的? / p>
这是一种模式,其中 module.exports
设置为一个功能。 要求该模块返回一个函数,而require之后的括号将使用参数来计算函数
在上面的示例中,您的 ./ routes / index.js
文件将如下所示:
module.exports = function(app){
app.get('/',function(req,res ){
});
// ...
};
此模式通常用于将变量传递给模块,如上所述,使用 app
变量。
I know what this require statement does.
var express = require('express');
var app = express();
But sometimes i have seen two parentheses after the require.
var routes = require('./routes')(app);
Q) What does this mean, and how does it work?
This is a pattern in which the module.exports
of the module is set to a function. Requiring that module returns a function, and the parentheses after the require evaluates the function with an argument.
In your example above, your ./routes/index.js
file would look something like the following:
module.exports = function(app) {
app.get('/', function(req, res) {
});
// ...
};
This pattern is often used to pass variables to modules, as can bee seen above with the app
variable.
这篇关于双括号是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!