Is there any way to 'get' a class/function/variable that's declared inside a function? example:
() => {
class foo{
}
const bar = () =>
{
console.log("Can print from outside?")
}
}
//Any way to make this possible?:
console.log(new foo())
bar()
Note: Can't change the anonymous function (injection related)
JS is lexical scope, you can't access a local variable outside its function. you may wanna check Closures
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures
This is really hacky!!, but if you don't mind another instance of the code you can just get the current source and wrap inside a function that exposes the foo & bar..
eg..
const anom = () => {
class foo{
}
const bar = () =>
{
console.log("Can print from outside?")
}
}
let code = anom.toString();
code =
code.slice(
code.indexOf('{') + 1,
code.lastIndexOf('}')
) + 'return {foo, bar}';
const f = new Function(code);
const {foo, bar} = f();
bar();
var fooInst = new foo();
console.log(fooInst);