0

my code:

function number(a){

return a
}

function x() {
    return new Function("return number('22')")()
}
console.log(x())

I am experimenting with calling a function from a function using the new Function()() now when running this piece of code in a browser it is working just fine but when i run it in webstorm it throws the error below:

undefined:3 return number('22') ^

ReferenceError: number is not defined at eval (eval at x (C:\Users\WebstormProjects\tests.js:11:12), :3:1) at x (C:\Users\WebstormProjects\tests.js:11:47) at Object. (C:\Users\WebstormProjects\tests.js:13:13) at Module._compile (internal/modules/cjs/loader.js:955:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10) at Module.load (internal/modules/cjs/loader.js:811:32) at Function.Module._load (internal/modules/cjs/loader.js:723:14) at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10) at internal/main/run_main_module.js:17:11

L4W
  • 31
  • 2
  • 2
    I'm guessing that it's sandboxing eval so that your number function isn't in scope. Also, obligatory warning: *never use eval or the function constructor*. There is almost no legitimate reason to ever use them ever. – Jared Smith May 21 '20 at 11:42
  • The code in a `new Function()` is global-scoped, but your `number` is not – Bergi May 21 '20 at 11:42
  • 1
    @JaredSmith No, it's just scoping the *tests.js* module. No sandboxing. – Bergi May 21 '20 at 11:43
  • everything runs in test.js and the functions used are both just in the file they are not in any object etc – L4W May 21 '20 at 15:29

0 Answers0