I have class Foo, and want to make a function FooPrime which is equivalent to call new Foo() with the arguments passed to FooPrime;
In other words, if I do:
FooPrime(1, 2, 3);
it should be the same as calling to:
new Foo(1, 2, 3)
Normally I'd use call or apply for this sort of thing, and I can get almost the same effect using apply:
var FooPrime = function() {
return Foo.apply({}, arguments);
}
However:
Foo.apply({}, [1, 2, 3]);
is not exactly the same as calling:
new Foo(1, 2, 3)
so my question is, is there any way to make a function which does exactly the same thing as new, with any number of arguments?
new (Function.prototype.bind.apply(Foo, [null].concat(argsArray)))();(borrowed from babel)