views:

158

answers:

2

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

+3  A: 

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]();
}
Annabelle
First, thanks for the response. As always after looking over an answer I feel a bit stupid for having missed some (in retrospect fairly obvious) things. In this case, strings are easier, however passing the constructor (something I didn't know could be done in that way) looks lightly neater and more importantly clear. I am not dead set against the use of eval, and as you say it has its place, however experience has taught me that if I think I need to use it 99% of the time I don't understand the language well enough. (This is one of those times).
Yacoby
@Yacoby well said! :) Especially the last sentences. I wish everyone would think this way.
galambalazs
This was a great answer. Learned something new about javascript! Thanks :)
Bryan Ross
+2  A: 

You were at the gate of completeness. While Annabelle's solution let's you to do what's you've just wanted in the way you wanted (passing strings), let me offer you an alternative. (passing function references)

function Foo(){}
function create(name){
    return new name();
}
create(Foo); // IS equivalent to new Foo();

And voila, it works :) I told you. You were at the doorsteps of the solution.

What happened is that you've try to do this

new 'Foo'()

Which doesn't makes much sense, does it? But now you pass the function by reference so the line return new name(); will be transformed into return new Foo(); just how you would expect.

And now the doors are opened to abstract the asynchronousness of your application. Have fun!

Appendix: Functions are first-class objects, which means that they can be stored by reference, passed as an argument by reference or returned by another function as values.

galambalazs
The second and third parameters to `require()` still have to be strings, the objects they identify are properties of objects that do not exist yet.
Annabelle
Yes you're right.
galambalazs