I had to do something similar and used an approach that looks
something like this
public function CustomObject(argsObject:Object)
{ for (var arg:String in argsObject)
{ switch (arg)
{ case somePropName:
//code for a property that needs special handling
break;
case aStringProp:
case anotherStringProp:
// code for props that need type checking
if (argsObject[arg] is String)
{ this[arg] = argsObject[arg];
}
else
{ //handle type error
}
break;
case aProp:
case anotherProp:
// code for propS that doesn't need type checking
this[arg] = argsObject[arg];
break;
default:
//handle properties that aren't in the custom obj
}
}
}
In my case
--- In [email protected], "Thomas Viktil" <[EMAIL PROTECTED]> wrote:
>
> Hello!
>
> I'm building a class which has a constructor that is supposed to
fill the
> properties of the class based on the content of the object submitted
to the
> constructor. To better explain, here's how it is supposed to work:
>
> var argsObj:Object = new Object();
> argsObj.property1 = "hello";
>
> var myObject:customObject = new customObject(argsObj);
>
> customObject has a property called property1, and so does argsObj.
>
> Now, the question is; how can I, in a simple way, put what ever is
in the
> argsObj in the correct place in my customObject? What will the
constructor
> look like?
>
> Should I use setter functions, og set the property directly
(this.property =
> arg)? I can see the advantage of using setter's. I could easily
sense if one
> property is not of the correct type, and then do something. But,
maybe I can
> to that in the constructor?
>
>
> This wasn't easy to explain, but I hope you understand.
>
> Regards
> Thomas Viktil
>