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

382
Views
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 answers
Answer question

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 Report

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 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!