1 | var $ = require('../internals/export');
|
---|
2 | var getBuiltIn = require('../internals/get-built-in');
|
---|
3 | var aFunction = require('../internals/a-function');
|
---|
4 | var anObject = require('../internals/an-object');
|
---|
5 | var isObject = require('../internals/is-object');
|
---|
6 | var create = require('../internals/object-create');
|
---|
7 | var bind = require('../internals/function-bind');
|
---|
8 | var fails = require('../internals/fails');
|
---|
9 |
|
---|
10 | var nativeConstruct = getBuiltIn('Reflect', 'construct');
|
---|
11 |
|
---|
12 | // `Reflect.construct` method
|
---|
13 | // https://tc39.es/ecma262/#sec-reflect.construct
|
---|
14 | // MS Edge supports only 2 arguments and argumentsList argument is optional
|
---|
15 | // FF Nightly sets third argument as `new.target`, but does not create `this` from it
|
---|
16 | var NEW_TARGET_BUG = fails(function () {
|
---|
17 | function F() { /* empty */ }
|
---|
18 | return !(nativeConstruct(function () { /* empty */ }, [], F) instanceof F);
|
---|
19 | });
|
---|
20 | var ARGS_BUG = !fails(function () {
|
---|
21 | nativeConstruct(function () { /* empty */ });
|
---|
22 | });
|
---|
23 | var FORCED = NEW_TARGET_BUG || ARGS_BUG;
|
---|
24 |
|
---|
25 | $({ target: 'Reflect', stat: true, forced: FORCED, sham: FORCED }, {
|
---|
26 | construct: function construct(Target, args /* , newTarget */) {
|
---|
27 | aFunction(Target);
|
---|
28 | anObject(args);
|
---|
29 | var newTarget = arguments.length < 3 ? Target : aFunction(arguments[2]);
|
---|
30 | if (ARGS_BUG && !NEW_TARGET_BUG) return nativeConstruct(Target, args, newTarget);
|
---|
31 | if (Target == newTarget) {
|
---|
32 | // w/o altered newTarget, optimization for 0-4 arguments
|
---|
33 | switch (args.length) {
|
---|
34 | case 0: return new Target();
|
---|
35 | case 1: return new Target(args[0]);
|
---|
36 | case 2: return new Target(args[0], args[1]);
|
---|
37 | case 3: return new Target(args[0], args[1], args[2]);
|
---|
38 | case 4: return new Target(args[0], args[1], args[2], args[3]);
|
---|
39 | }
|
---|
40 | // w/o altered newTarget, lot of arguments case
|
---|
41 | var $args = [null];
|
---|
42 | $args.push.apply($args, args);
|
---|
43 | return new (bind.apply(Target, $args))();
|
---|
44 | }
|
---|
45 | // with altered newTarget, not support built-in constructors
|
---|
46 | var proto = newTarget.prototype;
|
---|
47 | var instance = create(isObject(proto) ? proto : Object.prototype);
|
---|
48 | var result = Function.apply.call(Target, instance, args);
|
---|
49 | return isObject(result) ? result : instance;
|
---|
50 | }
|
---|
51 | });
|
---|