Calling a constructor with an array of arguments

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

obj.foo(1,2,3);

you can do

obj.apply(obj, [1,2,3]);

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: