This code works:
it.cb(h => { console.log(h); h.ctn(); }); it.cb(new Function( 'h', [ 'console.log(h)', 'h.ctn()' ] .join(';') ));
these two tests are basically identical. But building a string with such an array is cumbersome, and you cannot get static analysis. So what I was going to do was something like this:
it.cb(isolated(h => { console.log(h); h.ctn(); }));
where isolated is an auxiliary function that looks something like this:
const isolated = function(fn){ const str = fn.toString(); const paramNames = getParamNames(str); return new Function(...paramNames.concat(str)); };
The biggest problem is that Function.prototype.toString() provides you with the whole function. Does anyone know a good way to simply get the body of a function from a string representation of a function?
Update: Poberts asked what the purpose of this is, the goal is simple:
const foo = 3; it.cb(isolated(h => { console.log(foo);
source share