双括号是什么意思 [英] What does double parentheses mean in a require

查看:919
本文介绍了双括号是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这需要什么声明。

  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屋!

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