Skip to content

Instantly share code, notes, and snippets.

@beeryardtech
Last active September 5, 2017 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beeryardtech/14ef8fb243bc9de3605cfecbb6a62209 to your computer and use it in GitHub Desktop.
Save beeryardtech/14ef8fb243bc9de3605cfecbb6a62209 to your computer and use it in GitHub Desktop.
How to create a dynamic constructor without needing caller to use "new"
function X(a, b, c) {
console.log(a, b, c);
}
var c = (n, Fn) => {
return function() {
var a = _.toArray(arguments);
var t = _.times(n, (n) => {
return 'a[' + n + ']'
});
eval('new Fn(' + t.join(', ') + ');');
};
};
c(3, X)('aa', 'bb', 'cc');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment