0

For example, if I have this:

var foo = 444;

var x = new Function("console.log('5'); console.log(foo);");

x();

It says foo is undefined. I want to be able to access foo and many other global variables within this new Function context.

One solution is:

var foo = 444;

var x = new Function('foo', "console.log('5'); console.log(foo);");

x(foo);

But this requires passing everything through as separate parameters. Too cumbersome.

The only alternative I can think of is have a container object hold every single variable and then only pass that container:

var container = { };

container.foo = 444;

var x = new Function('container', "console.log('5'); console.log(container.foo);");

x(container);

But that requires having to put every one of my variables in to the container.

I can't do something like this:

var x = new Function('container', "console.log('5'); console.log(" + foo + ");");

because I need to evaluate foo at the time of the function exeuction. Not when x is declared.

I know using new Function uses eval and it's generally evil. I'm writing a code parser and I'm trying to optimize it so I'm using it anyway.

Chron Bag
  • 587
  • 4
  • 17
  • _“But that requires having to put every one of my variables in to the container.”_ — the shortest syntax is `var container = {foo: 444};`. No need for all those `container.`… `=` …`;` lines. – Sebastian Simon Jul 20 '16 at 04:15
  • @Xufox: Yeah, I'm saying that `foo` is not the only variable I'll need to access. Probably 20+ – Chron Bag Jul 20 '16 at 04:23
  • You *can* access global variables in the `new Function` code. If you cannot access `foo`, then that only suggests that `foo` is not global. In what environment are you executing that script? – Bergi Jul 20 '16 at 05:02
  • @Bergi: In a js file in node. I guess global isn't the right word. Script-wide? – Chron Bag Jul 20 '16 at 05:05
  • @ChronBag: If `foo` is only in your module scope, not global, then you're pretty much out of luck. You will need to explicitly pass the values you want to be accessible in the new function's scope, though you can of course [abstract that out into a helper function](https://stackoverflow.com/questions/9479046/is-there-any-non-eval-way-to-create-a-function-with-a-runtime-determined-name) – Bergi Jul 20 '16 at 05:08
  • @Bergi: Ok so I guess container is the only option then. – Chron Bag Jul 20 '16 at 05:15
  • @ChronBag: Whether container in one parameter or multiple values in separate parameters doesn't make a difference (and in the end, either can be programmatically constructed from the other) – Bergi Jul 20 '16 at 05:28

1 Answers1

-2

One simple solution is to put the variable on window

window.foo = 444;
window.foo2 = 555;
window.foo3 = 666;
var x = new Function("console.log('5'); console.log(window.foo);");
x();

Another option is use another function to provide you with the constant values

function globalContainer(){
    return {foo: 444, foo1: 555, foo2: 666};
}

var x = new Function("console.log('5'); console.log(globalContainer().foo);");
x();
Mursaleen Ahmad
  • 313
  • 2
  • 10