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

383
Vistas
best way to replace an eval( "new object()" )

I m looking for the best way to replace eval( "new object();" )

The ugly source :

    //var type = window.$(droppedDomNode).data("shape");
    var type = "sankey.shape.Start";

    var figure = eval("new "+type+"();");
    
    // create a command for the undo/redo support
    var command = new draw2d.command.CommandAdd(this, figure, x, y);

The eval create a new sankey.shape.Start object.

I'm looking for best solution to replace this kind of code.

Thanks

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

0

I'd build an object (mapping) of constructor functions.

const constructors = {
  "sankey.shape.Start": () => new sankey.shape.Start(),
  // etc... (this could be built programmatically)
};

Then,

const figure = constructors[type]();
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can split it and walk up the path from window if it is global

var foo = {
  bar: {
    world: 'baz',
  }
}

const getIt = (str, start = window) => {
  return str.split(/\./).reduce((o, k) => o[k], start);
}


const x = "foo.bar.world";
console.log(getIt(x))

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