wallet-core/node_modules/core-js/modules/es6.reflect.construct.js

48 lines
1.9 KiB
JavaScript
Raw Normal View History

2016-10-10 03:43:44 +02:00
// 26.1.2 Reflect.construct(target, argumentsList [, newTarget])
2017-08-14 05:01:11 +02:00
var $export = require('./_export');
var create = require('./_object-create');
var aFunction = require('./_a-function');
var anObject = require('./_an-object');
var isObject = require('./_is-object');
var fails = require('./_fails');
var bind = require('./_bind');
var rConstruct = (require('./_global').Reflect || {}).construct;
2016-10-10 03:43:44 +02:00
2017-05-27 17:36:13 +02:00
// MS Edge supports only 2 arguments and argumentsList argument is optional
2016-10-10 03:43:44 +02:00
// FF Nightly sets third argument as `new.target`, but does not create `this` from it
2017-08-14 05:01:11 +02:00
var NEW_TARGET_BUG = fails(function () {
function F() { /* empty */ }
return !(rConstruct(function () { /* empty */ }, [], F) instanceof F);
2017-05-27 17:36:13 +02:00
});
2017-08-14 05:01:11 +02:00
var ARGS_BUG = !fails(function () {
rConstruct(function () { /* empty */ });
2017-05-27 17:36:13 +02:00
});
$export($export.S + $export.F * (NEW_TARGET_BUG || ARGS_BUG), 'Reflect', {
2017-08-14 05:01:11 +02:00
construct: function construct(Target, args /* , newTarget */) {
2016-10-10 03:43:44 +02:00
aFunction(Target);
anObject(args);
var newTarget = arguments.length < 3 ? Target : aFunction(arguments[2]);
2017-08-14 05:01:11 +02:00
if (ARGS_BUG && !NEW_TARGET_BUG) return rConstruct(Target, args, newTarget);
if (Target == newTarget) {
2016-10-10 03:43:44 +02:00
// w/o altered newTarget, optimization for 0-4 arguments
2017-08-14 05:01:11 +02:00
switch (args.length) {
case 0: return new Target();
2016-10-10 03:43:44 +02:00
case 1: return new Target(args[0]);
case 2: return new Target(args[0], args[1]);
case 3: return new Target(args[0], args[1], args[2]);
case 4: return new Target(args[0], args[1], args[2], args[3]);
}
// w/o altered newTarget, lot of arguments case
var $args = [null];
$args.push.apply($args, args);
2017-08-14 05:01:11 +02:00
return new (bind.apply(Target, $args))();
2016-10-10 03:43:44 +02:00
}
// with altered newTarget, not support built-in constructors
2017-08-14 05:01:11 +02:00
var proto = newTarget.prototype;
var instance = create(isObject(proto) ? proto : Object.prototype);
var result = Function.apply.call(Target, instance, args);
2016-10-10 03:43:44 +02:00
return isObject(result) ? result : instance;
}
2017-08-14 05:01:11 +02:00
});