PDA

View Full Version : Dynamicly naming custom object properties


Vic D
12-27-2002, 07:16 PM
Is there any way to assign properties to a custom object without having their names / numbers predetermined?

I want to give a string (ex: "param1=value1") to an object constructor and have the contructor create an object with a property called "param1" whose value is "value1".

It seems that the property names have to be constants!!!

Please help.

Here is the code so far:

function HandyObject()
{
var InputStr = HandyObject.arguments[0];
var InputPairs = InputStr.split(":");
for( i=0; i<InputPairs.length; i++ )
{
var NameValue = InputPairs[i].split("=");
var ParamName = new String(NameValue[0]);
var ParamValue = new String(NameValue[1]);
eval( this.ParamName = ParamValue );
}
}

var MyObj = new HandyObject("Param1=hi:Param2=there");

for( var PropertyName in MyObj )
{
alert( PropertyName + " = " + MyObj[PropertyName] );
}

jkd
12-27-2002, 07:32 PM
myobject[somestring] = bla

somestring can be a variable string.

Vic D
12-27-2002, 07:37 PM
Damn, jkd... Damn good. Thanks. I will bookmark this forum!