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] );
}
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] );
}