I asked internally and the equivalent createInstance functionality is
not in the ECMA spec.  So probably not going to come from us in Flex 2.
Please file the enhancement request at
http://www.macromedia.com/go/wish.

You would need to simulate by creating the object with a no-arg
constructor and then having a well-known function like "initArgs" which
you could call via Function.apply.

public function createInstance (className:String, args:Array) : Object {
    var myClass : Class = getClassByName(className);
    var inst : Object = new myClass();
    inst.initArgs.apply(inst, args);
    return inst;
}

Matt

-----Original Message-----
From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Jens Halm
Sent: Wednesday, January 18, 2006 10:32 AM
To: Brendan Meutzner
Subject: Re: [flexcoders] addChild from String


> Hi All,

> I'd like to provide a String value which represents the DisplayObject
> class I want to create dynamically with addChild.  Has anyone
> accomplished this?

You mean like this?

public function createInstance (className : String) : Object {
    var MyClass : Class = getClassByName(className);
    return new MyClass();
}


Btw.: what I really miss is a method createInstance(args : Array) in
the Class class, so I could easily provide an arbitrary number of
constructor arguments not known until runtime, like this:
    
public function createInstance (className:String, args:Array) : Object {
    var myClass : Class = getClassByName(className);
    return myClass.createInstance(args);
}

Class.createInstance(args:Array) would be the constructor equivalent
of Function.apply(scope:Object, args:Array). If I use Function.apply
after I created an instance the constructor would be called twice
which would be quite dirty.

Consider this as a feature request...  ;)


Jens
www.oregano-server.org




--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives:
http://www.mail-archive.com/flexcoders%40yahoogroups.com 
Yahoo! Groups Links



 





--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com 
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/
 



Reply via email to