Calling a constructor with an array of arguments

1 June 2010

Some­times it’s useful to use JavaScript’s apply() func­tion to call a func­tion that usually takes sep­a­rate ar­gu­ments with a single array ar­gu­ment instead. That is, instead of

you can do

Unfortunately, there’s no way to use apply() with a constructor. However, it turns out that you can exploit the fact that con­struc­tors can be chained using apply(...) to achieve this, al­though this re­quires the cre­ation of a proxy class. The construct() func­tion below lets you do:

The construct() function:

Some sample code that uses it: