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

186
Vistas
How to remove new Function with Anonymous function in javascript

I am trying to replace eval/new Function with corresponding anonymous function.

Existing Code -

var y = 2
var fn = new Function("return" +y)
console.log(fn)

When i print fn output is

ƒ anonymous(
) {
return2
}

Refactored code which i am writing -

var y = 2
var fn1 = function()  {return y}
console.log(fn1)

But fn1 in this case is

ƒ ()  {return y}

Any pointers how can i get the same output as fn ƒ anonymous() {return2} using my own anonymous function.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can't (at least not without using another version of eval).

You can get the same effect (i.e. not having a mutable y) by using a closure.

var y = 2
var fn1 = function(closure_y) {
  return function() {
    return closure_y;
  }
}(y)
y = 3;
console.log(fn1())

If your code depends on a function stringifying itself in a particular way, then you should probably rewrite the code so it no longer depends on that.

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