Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Jscript ➜ using a string to make a new object

using a string to make a new object

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Kyrock   (20 posts)  Bio
Date Mon 17 Nov 2008 08:20 PM (UTC)
Message
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);
}
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Mon 17 Nov 2008 08:34 PM (UTC)
Message
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.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Kyrock   (20 posts)  Bio
Date Reply #2 on Mon 17 Nov 2008 10:20 PM (UTC)
Message
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
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


14,565 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.