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)

4
  • define a global function by adding it to ... global ... e.g global.yourFunctionName = function (arg1, arg2, ... , argN) { function body goes here }; Commented Sep 26, 2016 at 1:06
  • no because i use this function client side too, I can't add global.foo Commented Sep 26, 2016 at 1:15
  • well, that would be useful information in the question :p Commented Sep 26, 2016 at 1:16
  • I try to reduce the problem more simply ^^ but if necessary I can developed^^ Commented Sep 26, 2016 at 1:20

2 Answers 2

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();

Sign up to request clarification or add additional context in comments.

4 Comments

What the hell... This works ! but I don't undersand why?
@Matrix see for example this question or this question
your answer is amazing, it should be award you like 10 good answer! XD
@Matrix looking at the second question i referenced 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.
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

5 Comments

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 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?
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 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)
@Matrix If you really want to use eval, and foo is declared in the global context in your other file: after evaling it in your function, just go global.foo = foo;, as I have reflected in the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.