Workarounds for dynamic invocation of Class constructors

by troy on December 4, 2008

Was looking into ObjectPooling and IOC, and the subject of dynamic invocation of Class constructors came up. I was unpleasantly surprised to find out that Class constructors aren't functions so can't have Function.apply used with them. However the '...(rest)' can make it similar to calling new functions. Here's a comparison of 2 workarounds in use.

Try Richard Lord's construct

or My new2

			trace("construct " + construct(Date, [2001,1,1]));
			trace("new2 " + new2(Date, 2001,1,1));

It's cool how many people into FDT are tackling problems of similar complexity. It's like having a clan.

{ 3 comments… read them below or add one }

Vertex January 30, 2009 at 4:14 pm

Hi, seems it is pretty cool!
Can you describe the code under “construct” and “new2″ functions?
Do you use it e.g. for new2 function:
new rest[0](rest[1], rest[2], rest[3]);
or it’s a bit more tricky?

Nate Austin November 20, 2009 at 2:26 am

lolol

Yeah I just had to write this myself.
High five.

troy November 20, 2009 at 3:35 am

Hey Nate! LOL then of course, you and the other 5 people on this planet that care about such esotaric things totally rock!

Leave a Comment

Previous post:

Next post: