Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

354
Views
'hook' types out of anonymous functions

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)

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report

0

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);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!