2016-10-10 03:43:44 +02:00
|
|
|
// 26.1.2 Reflect.construct(target, argumentsList [, newTarget])
|
2017-04-20 03:09:25 +02:00
|
|
|
var $ = require('./$')
|
|
|
|
, $export = require('./$.export')
|
|
|
|
, aFunction = require('./$.a-function')
|
|
|
|
, anObject = require('./$.an-object')
|
|
|
|
, isObject = require('./$.is-object')
|
|
|
|
, bind = Function.bind || require('./$.core').Function.prototype.bind;
|
2016-10-10 03:43:44 +02:00
|
|
|
|
2017-04-20 03:09:25 +02:00
|
|
|
// MS Edge supports only 2 arguments
|
2016-10-10 03:43:44 +02:00
|
|
|
// FF Nightly sets third argument as `new.target`, but does not create `this` from it
|
2017-04-20 03:09:25 +02:00
|
|
|
$export($export.S + $export.F * require('./$.fails')(function(){
|
2016-10-10 03:43:44 +02:00
|
|
|
function F(){}
|
2017-04-20 03:09:25 +02:00
|
|
|
return !(Reflect.construct(function(){}, [], F) instanceof F);
|
|
|
|
}), 'Reflect', {
|
2016-10-10 03:43:44 +02:00
|
|
|
construct: function construct(Target, args /*, newTarget*/){
|
|
|
|
aFunction(Target);
|
|
|
|
anObject(args);
|
|
|
|
var newTarget = arguments.length < 3 ? Target : aFunction(arguments[2]);
|
|
|
|
if(Target == newTarget){
|
|
|
|
// w/o altered newTarget, optimization for 0-4 arguments
|
|
|
|
switch(args.length){
|
|
|
|
case 0: return new Target;
|
|
|
|
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);
|
|
|
|
return new (bind.apply(Target, $args));
|
|
|
|
}
|
|
|
|
// with altered newTarget, not support built-in constructors
|
|
|
|
var proto = newTarget.prototype
|
2017-04-20 03:09:25 +02:00
|
|
|
, instance = $.create(isObject(proto) ? proto : Object.prototype)
|
2016-10-10 03:43:44 +02:00
|
|
|
, result = Function.apply.call(Target, instance, args);
|
|
|
|
return isObject(result) ? result : instance;
|
|
|
|
}
|
|
|
|
});
|