Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 906139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:20:13+00:00 2026-05-15T16:20:13+00:00

I have a function that takes a string object name and I need the

  • 0

I have a function that takes a string object name and I need the function to create an new instance of a object that has the same name as the value of the string

For example,

function Foo(){}
function create(name){
    return new name();
}
create('Foo'); //should be equivalent to new Foo();

While I know this would be possible via eval, it would be good to try and avoid using it. I am also interested if anyone has an alternative ideas to the problem (below)


I have a database and a set of (using classical OO methodology) classes, roughly one for each table that define common operations on that table. (Very similar to Zend_Db for those who use PHP). As everything is asynchronous doing tasks based on the result of the last one can lead to very indented code

var table1 = new Table1Db();
table1.doFoo({
    success:function(){
        var table2 = new Table2Db();
        table2.doBar({
            notFound:function(){
                doStuff();
            }
        });
    }
});

The obvious solution is to create helper methods that abstracts the asynchronous nature of the code.

Db.using(db) //the database object
  .require('Table1', 'doFoo', 'success') //table name, function, excpected callback
  .require('Table2', 'doBar', 'notFound')
  .then(doStuff);

Which simplifies things. However the problem is that I need to be able to create the table classes, the names of which can be inferred from the first augment passed to require which leads me to the problem above…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:20:14+00:00Added an answer on May 15, 2026 at 4:20 pm

    Why not simply pass the constructor function into the require method? That way you sidestep the whole issue of converting from name to function. Your example would then look like:

    Db.using(db) //the database object
      .require(Table1Db, 'doFoo', 'success') //table constructor, function name, expected callback
      .require(Table2Db, 'doBar', 'notFound')
      .then(doStuff);
    

    However, if you really want to use a string…

    Why are you deadset on avoiding using eval? It is a tool in the language and every tool has its purpose (just as every tool can be misused). If you’re concerned about allowing arbitrary execution, a simple regular expression test should render your usage safe.

    If you’re dead-set on avoiding eval and if all of your constructor functions are created in the default global scope (i.e. the window object), this would work:

    function create(name) {
      return new window[name]();
    }
    

    If you want to get fancy and support namespace objects (i.e. create('MyCompany.MyLibrary.MyObject'), you could do something like this:

    function create(name) {
      var current,
          parts,
          constructorName;
    
      parts = name.split('.');
      constructorName = parts[parts.length - 1];
      current = window;
      for (var i = 0; i < parts.length - 1; i++) {
        current = current[parts[i]];
      }
    
      return new current[constructorName]();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 458k
  • Answers 459k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Compare the execution plan for the query with and without… May 15, 2026 at 11:17 pm
  • Editorial Team
    Editorial Team added an answer JSON with Padding works by adding a script element to… May 15, 2026 at 11:17 pm
  • Editorial Team
    Editorial Team added an answer There is a way: Thread.currentThread().getStackTrace() But there should not be… May 15, 2026 at 11:17 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.