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.
Say I have something along the lines of the following in a file:
fun开发者_开发技巧ction foo() {}
function bar() {}
then:
- Are the functions saved in some global object?
- If not, how can I access these functions without knowing their names? Can I iterate through all existing functions and obtain them in such a way?
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
You want to get access to the current scope object but it's impossible in JavaScript.
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.
精彩评论