0

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:

function foo() {}
function bar() {}

then:

  1. Are the functions saved in some global object?
  2. 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?
pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • Have you tried `this`? (The global `this`, not inside an instantiated class) – Brett Zamir Jun 04 '11 at 13:28
  • @brettz9: I tried your suggestion: `function foo(){};console.log(this.foo)` but it returns `undefined` - or is this not what you mean? – pimvdb Jun 04 '11 at 13:29
  • That was my guess--surprised it doesn't work...Googling got me me this: "process is no longer the global object. GLOBAL is." – Brett Zamir Jun 04 '11 at 13:31
  • @brettz9: `this.foo`, `global.foo`, `process.foo` are all undefined unfortunately... – pimvdb Jun 04 '11 at 13:33
  • If helpful, there is also http://stackoverflow.com/questions/4133114/global-object-in-node-js and http://stackoverflow.com/questions/4133149/modifying-global-object-in-node-js – Brett Zamir Jun 04 '11 at 13:34
  • Since JS is case sensitive, and [some pages](http://davidwalsh.name/mootools-nodejs) say it is GLOBAL not global, you might try that too. Or, per one comment, maybe it is just a problem in node-repl environment. Other than that, I'm out of guesses. :) – Brett Zamir Jun 04 '11 at 13:38
  • [Other people](http://comments.gmane.org/gmane.comp.lang.javascript.nodejs/21513) have gotten `this` working with some version or other, though I know that might not help in your case. – Brett Zamir Jun 04 '11 at 13:39
  • @brettz9: No luck with `GLOBAL`, and it's not node-repl but just `node.exe` executed with a `.js` file. – pimvdb Jun 04 '11 at 13:40
  • @pimvdb try `module.foo` – Raynos Jun 04 '11 at 13:42
  • @Raynos: I cannot get your suggestion working either I'm afraid: `function foo(){};console.log(module.foo);` returns `undefined` as well. – pimvdb Jun 04 '11 at 13:43
  • @pimvdb your issue might be function declarations. Try `var foo = 42;console.log(module.foo);` – Raynos Jun 04 '11 at 13:47
  • @Raynos: Again `undefined`, even with just a number. – pimvdb Jun 04 '11 at 13:50
  • Just tested this myself in 0.4.8. You just can't do this anymore. @pimvdb There are extensions of nodejs you could install that would allow you to do this. But this basically means using C++ to extend javascript as a language. – Raynos Jun 04 '11 at 13:51

3 Answers3

1

You want to get access to the current scope object but it's impossible in JavaScript.

bjornd
  • 22,397
  • 4
  • 57
  • 73
  • I might be wrong but in a browser I can access `window` in this case. Is this not available in Node.js at all? – pimvdb Jun 04 '11 at 13:22
  • Take a look here http://stackoverflow.com/questions/4133114/global-object-in-node-js – bjornd Jun 04 '11 at 13:28
  • And actually what you want to do is against all the rules, I even heard somewhere people are killed for such things, even for attempts...:) P.S.: I mean polluting of the global scope. – bjornd Jun 04 '11 at 13:32
  • @bjornd: What I understand from your link is that the functions are 'global on their own', so no solution in that case, is that correct? – pimvdb Jun 04 '11 at 13:35
  • Why not to assign them to the module.exports right when you define them? – bjornd Jun 04 '11 at 13:37
  • @bjornd: Because I have to call them like `exports.foo` at all times, when used inside the file itself. – pimvdb Jun 04 '11 at 13:37
  • @pimvdb `var foo = exports.foo = function() { };` – Raynos Jun 04 '11 at 13:43
  • @Raynos: That's a possibility, thanks. However, is it correct that I cannot just code `function foo(){}` and add some code at the end to put all functions into `exports`? – pimvdb Jun 04 '11 at 13:45
  • 2
    @pimvdb Yes that is correct. In current versions of node.js you cannot do this. You do not have access to the [[Scope]] object. – Raynos Jun 04 '11 at 13:51
1

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

Raynos
  • 166,823
  • 56
  • 351
  • 396
1

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.

mgutz
  • 933
  • 1
  • 8
  • 7