using a string to make a new object

Posted by Kyrock on Mon 17 Nov 2008 08:20 PM — 3 posts, 15,571 views.

#0
Was wandering if there's a way to make an object type from a string. Like if I had a mush variable containing: "testObject1(),testObject2()"


function testObject1()
{
this.x = 20;
}
function testObject2()
{
this.x = 25;
}

function testing(list, element)
{
var listContents = world.getVariable(list);
var theList = listContents.split(",");
// right here. is there a way to do this?
var obj1 = new theList [element];
world.note(obj1.x);
}
USA #1
You could do it with an eval call (which means: take this string and interpret it like code), but there might be better ways of doing it.
#2
well isn't that a cool method! I changed it to:

function testing(list, element)
{
var listContents = world.getVariable(list);
var theList = listContents.split(",");
//var obj1 = new eval(theList [element]);
var toEval = "var obj1 = new " + theList [element];
eval(toEval);
world.note(obj1.x);
}

and it works, thanks David