1 | var baseCreate = require('./_baseCreate'),
|
---|
2 | isObject = require('./isObject');
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * Creates a function that produces an instance of `Ctor` regardless of
|
---|
6 | * whether it was invoked as part of a `new` expression or by `call` or `apply`.
|
---|
7 | *
|
---|
8 | * @private
|
---|
9 | * @param {Function} Ctor The constructor to wrap.
|
---|
10 | * @returns {Function} Returns the new wrapped function.
|
---|
11 | */
|
---|
12 | function createCtor(Ctor) {
|
---|
13 | return function() {
|
---|
14 | // Use a `switch` statement to work with class constructors. See
|
---|
15 | // http://ecma-international.org/ecma-262/7.0/#sec-ecmascript-function-objects-call-thisargument-argumentslist
|
---|
16 | // for more details.
|
---|
17 | var args = arguments;
|
---|
18 | switch (args.length) {
|
---|
19 | case 0: return new Ctor;
|
---|
20 | case 1: return new Ctor(args[0]);
|
---|
21 | case 2: return new Ctor(args[0], args[1]);
|
---|
22 | case 3: return new Ctor(args[0], args[1], args[2]);
|
---|
23 | case 4: return new Ctor(args[0], args[1], args[2], args[3]);
|
---|
24 | case 5: return new Ctor(args[0], args[1], args[2], args[3], args[4]);
|
---|
25 | case 6: return new Ctor(args[0], args[1], args[2], args[3], args[4], args[5]);
|
---|
26 | case 7: return new Ctor(args[0], args[1], args[2], args[3], args[4], args[5], args[6]);
|
---|
27 | }
|
---|
28 | var thisBinding = baseCreate(Ctor.prototype),
|
---|
29 | result = Ctor.apply(thisBinding, args);
|
---|
30 |
|
---|
31 | // Mimic the constructor's `return` behavior.
|
---|
32 | // See https://es5.github.io/#x13.2.2 for more details.
|
---|
33 | return isObject(result) ? result : thisBinding;
|
---|
34 | };
|
---|
35 | }
|
---|
36 |
|
---|
37 | module.exports = createCtor;
|
---|