Is there any syntax / API / idiom in JavaScript to get a function that instantiates a type for any type?
That is, replace the call new Type(..args)with, for example, (pseudocode) Type.new(...args)so that I can pass the object to the function as a parameter?
I tried using it on my const newTypeAsFn = new Typeown (without parentheses), but it still calls the constructor instead of returning a function object that can be passed as a parameter and called later.
This is an example of what I wanted to do:
const newStringAsFn = String.new;
[1, 2, 3].map(newStringAsFn);
// [String {"1"}, String {"2"}, String {"3"}]
I came up with a function that does this:
const newAsFn = (type) => (...args) => new type(...args);
This can be used as follows:
const newStringAsFn = newAsFn(String);
[1, 2, 3].map(newStringAsFn);
(, ) , ? ? ?