问题描述
我正在阅读一个项目的一些代码来学习 node.js 然后我找到了这行 (debug = require('debug')('api:server')
),它被括在括号中.由于我是编程新手,当我不知道某些东西时,我只是在网上搜索它,但我找不到这个问题的答案.如果您要让我更积极地在网络上进行搜索,请也告诉我如何.
I was reading some code of a project to learn node.js then I found this line (debug = require('debug')('api:server')
) which is enclosed in brackets. As I'm new to programming and when I don't know something I just search it on the web, but I couldn't find an answer for this one. If you are going to tell me to search on the web more aggressively then please tell me HOW too.
推荐答案
require
返回某个其他模块的导出.在这里,由于 debug
被传递到 require
,debug
模块是必需的.这个模块的作用是:
require
returns the exports of some other module. Here, since debug
is being passed into require
, the debug
module is being required. What this module does is:
debug 暴露一个函数;只需将此函数传递给您的模块的名称,它就会返回一个装饰版本的 console.error 供您将调试语句传递给.这将允许您切换模块不同部分以及整个模块的调试输出.
所以
const debug = require('debug')('api:server');
其中 require('debug')
解析为一个函数,就像:
where require('debug')
resolves to a function, is like:
const debug = deccorateModule('api:server');
其中 decorateModule
执行上述功能.在这种情况下,require
充当高阶函数:返回函数的函数.(你可能有一个名为 api:server
的模块)
where decorateModule
carries out the functionality described above. In this case, require
acts as a higher-order function: a function which returns a function. (You may likely have a module named api:server
)
这导致 debug
变量保存了 console.error
的修饰版本.
This results in the debug
variable holding the decorated version of console.error
.
这篇关于“debug = require('debug')('api:server')"是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!