Is this the answer to the age old question of 'Who's on first?' :) -----Original Message----- From: JesterXL [mailto:[EMAIL PROTECTED] Sent: Monday, April 04, 2005 10:35 PM To: flexcoders@yahoogroups.com Subject: Re: [flexcoders] Re: AS - adding dynamic properties to an object
A rundown of them all: "Who's on here?" ----------------- function showProps(o:Object):Void { for(var p in o) { trace("-------------"); trace(p + ": " + o[p]); trace("typeof: " + typeof(o[p])); trace("is a String: " + (o[p] instanceof String)); trace("is a Number: " + (o[p] instanceof Number)); trace("is a Array: " + (o[p] instanceof Array)); trace("is a Boolean: " + (o[p] instanceof Boolean)); trace("is a Date: " + (o[p] instanceof Date)); } } Obviously there are more data types. You know the type (not recommended): var val:String = o[p]; You coerce the type (recommended, but expect weird results): var val:MyClass = MyClass(o[p]); Keep in mind, the above works differently since some intrinsic datatypes have special constructors; String(something) actually converts it to a String, and Date's overloaded with a few different ways of making them. ...obviously, interfaces are typically the best way, since you can typecast 'em (I think, I did it with a CollectionItr): var myA:Iterator = Interator(reallyARecordSet); Or something to that effect. ----- Original Message ----- From: "Ben Elmore" <[EMAIL PROTECTED]> To: <flexcoders@yahoogroups.com> Sent: Monday, April 04, 2005 10:15 PM Subject: RE: [flexcoders] Re: AS - adding dynamic properties to an object A quick way to do it (ugly admittedly) is to use the 'instanceof' or 'typeof' operator. if ( obj[propertyName] instanceof Date ) You might need to cast into number or date before you check the instance of to see if it is valid. -----Original Message----- From: viraf_bankwalla [mailto:[EMAIL PROTECTED] Sent: Monday, April 04, 2005 10:06 PM To: flexcoders@yahoogroups.com Subject: [flexcoders] Re: AS - adding dynamic properties to an object Thanks - the example below would have worked if I had knowledge of the property name when I wrote the code. This is not the case (the property name is retrieved with a service request). The solution that I found was to use the associative array syntax - obj[propertyName] = yy; Now the next question is that how do I specify that "propertyName" is of a given type - say Date or Number. Thanks. --- In flexcoders@yahoogroups.com, Manish Jethani <[EMAIL PROTECTED]> wrote: > On Apr 5, 2005 1:08 AM, viraf_bankwalla <[EMAIL PROTECTED]> wrote: > > > How do I add dynamic properties to an obect ? These properties are > > received over the HTTPService request. > > If you have an instance of a dynamic class, you can just stick them > onto the object. > > var o:Object = new Object(); // dynamic object > o.newProperty = "default value"; // stick a new property onto it > > Pretty much it. > > Manish Yahoo! Groups Links Yahoo! Groups Links Yahoo! Groups Links Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/flexcoders/ <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/