[英]What is the use of () post require() in node.js?
I understand require()
is used to import a particular node module into another and use it. 我知道
require()
用于将特定的节点模块导入另一个模块并使用它。 But what is the use of using additional ()
post that. 但是使用extra
()
的作用是什么。 Say i have following require statement in my server.js
file : var init = require('./config/init');
说我在
server.js
文件中有以下require语句: var init = require('./config/init');
. 。 It just means that we are importing modules declared in
./config/init.js
file. 这只是意味着我们要导入在
./config/init.js
文件中声明的模块。 But what does var init = require('./config/init')();
但是什么是
var init = require('./config/init')();
mean ? 意思 ? What is the use of () at the end of
require
statement ? require
语句结尾的()有什么用?
Simply that if this line imports a function
: 简单地说,如果此行导入一个
function
:
var init = require('./config/init')
...then the additional ()
executes that function immediately. ...然后extra
()
立即执行该功能。
Think of it as shortcut for these 2 lines 认为这是这两行的捷径
var initLib = require('./config/init')
var init = initLib();
require('library')
returns the exports of the specified library. require('library')
返回指定库的导出 。 In this case, it looks like the library returns a function, and you're simply calling that and storing the value returned from the function. 在这种情况下,库似乎返回了一个函数,而您只是在调用它并存储从函数返回的值。
In this case the module you're importing has code like this: 在这种情况下,您要导入的模块具有如下代码:
module.exports = function() {
...
return theInitValue;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.