[英]Obtain all functions in a file in Node.js
I have some functions inside a file.我在文件中有一些函数。 I'm trying to obtain all functions in that file, from within that file.
我正在尝试从该文件中获取该文件中的所有函数。 Normally, all functions are in the
window
object, but I'm using Node.js, which does not seem to have a window
object. Normally, all functions are in the
window
object, but I'm using Node.js, which does not seem to have a window
object.
Say I have something along the lines of the following in a file:假设我在文件中有以下内容:
function foo() {}
function bar() {}
then:然后:
You want to get access to the current scope object but it's impossible in JavaScript.您想访问当前的 scope object 但在 JavaScript 中是不可能的。
The following is a common pattern下面是一个常见的模式
var foo = exports.foo = function() {
// ...
}
This way its written to exports
and you can access it locally as foo
这样它就被写入了
exports
,您可以在本地以foo
的身份访问它
Your functions are wrapped in a closure.你的函数被包裹在一个闭包中。 Remember, node wraps file modules within something like this
请记住,节点将文件模块包装在这样的东西中
var module = { exports: {}};
(function(module, exports){
// your file module content
})(module, module.exports);
They are locals.他们是当地人。 Assign functions to an object,
exports
or global
to enumerate them.将函数分配给 object、
exports
或global
以枚举它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.