In a top-level, node.js console application, I have this:
var obj1 = { a: 1 };
var obj2 = { a: 2 };
var funcA = function () { return obj1.a + obj2.a };
var funcB = new Function("return obj1.a + obj2.a");
console.log(funcA()); // => 3
console.log(funcB()); // => ReferenceError: obj1 is not defined
Functions created by the Function constructor (funcB) are documented to "only be able to access their own local variables and global ones", but obj1 is global, and it cannot see it.
I need some way to get funcB to behave like funcA. It is ok to add code before the return, and/or add arguments to funcB, but I am trying to find a way that does not alter or require parsing the source after the return. I feel I need to understand why it can't even see the globals first.