0
context = this
function test() {
  (function(cmd) {
    eval(cmd);
  }).call(context, 'function foo(){}');
};

test();
foo(); // => ReferenceError: foo is not defined

how can I define a global function inside a function ? (using nodeJS)

Matrix
  • 3,458
  • 6
  • 40
  • 76

2 Answers2

1

A typical way to access the global object is calling a yielded value, e.g. from the comma operator.

function a() {
  (0, function () {
    this.foo = function () { console.log("works"); };
  })();
}
a();
foo();

UPDATE: Due to strict mode issues, here is another version (references: (1,eval)('this') vs eval('this') in JavaScript?, Cases where 'this' is the global Object in Javascript):

"use strict";
function a() {
  (0, eval)('this').foo = function () { console.log("works"); };
}
a();
foo();
Community
  • 1
  • 1
ASDFGerte
  • 4,695
  • 6
  • 16
  • 33
  • What the hell... This works ! but I don't undersand why? – Matrix Sep 26 '16 at 01:36
  • @Matrix see for example [this question](http://stackoverflow.com/questions/4295175/cases-where-this-is-the-global-object-in-javascript) or [this question](http://stackoverflow.com/questions/9107240/1-evalthis-vs-evalthis-in-javascript/9107304) – ASDFGerte Sep 26 '16 at 01:40
  • your answer is amazing, it should be award you like 10 good answer! XD – Matrix Sep 26 '16 at 01:51
  • 1
    @Matrix looking at the [second question i referenced](http://stackoverflow.com/questions/4295175/cases-where-this-is-the-global-object-in-javascript) the comma seems even unnecessary here, a simple IIFE would be enough. However, the previously posted code would fail in `strict mode` on ES5. Therefore i added an update, which should also work on `strict mode`. – ASDFGerte Sep 26 '16 at 02:16
0

Use the global object in Node.JS:

function test() {
  eval('function foo() { return "this is global"; }');
  global.foo = foo;
};

test();

console.log(foo()); // this is global
wilsonzlin
  • 2,154
  • 1
  • 12
  • 22
  • I can't do this. Foo() is define in other file and be used server AND client side, it can't change, it define by `function foo()`, can't add "global" – Matrix Sep 26 '16 at 01:19
  • @Matrix How is `Foo` defined in your other file? Is it global? Is it loaded alongside this script? Can you export the `Foo` function and `require` it in this script? – wilsonzlin Sep 26 '16 at 01:22
  • in other file is just define global like `function foo(){..}`. I don't use require() because it need syntaxe `module.export` (this is incompatible with client side), so I read file and eval() it – Matrix Sep 26 '16 at 01:25
  • @Matrix to make it compatible on the client side, just use `if (typeof exports != "undefined") module.exports = Foo`. It's recommended not to use `eval` as it's slow and sometimes dangerous (also the contexts get wacky, as you've found out) – wilsonzlin Sep 26 '16 at 01:26
  • @Matrix If you really want to use eval, and `foo` is declared in the global context in your other file: after `eval`ing it in your function, just go `global.foo = foo;`, as I have reflected in the answer – wilsonzlin Sep 26 '16 at 01:31