Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

363
Vistas
'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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda