Under normal conditions, new.target
will be either undefined
, the class of a constructor or a subclass or the function it is in or a subclass.
Supposedly, Reflect.construct(target, argumentsList, newTarget=target)
is equivalent to target.[[Construct]](CreateListFromArrayLike(argumentsList), newTarget)
. This allows us to set arbitrary Objects as new.target
. The [[Construct]]
internal method has this to say about newTarget
(The second argument):
The second argument is the object to which the new operator was initially applied.
So what does it mean for it to be a completely different value? (Especially for built-in functions)
For example, in the console:
>> Reflect.construct(String, [], String)
String { "", 1 more… }
>> Reflect.construct(String, [], Array)
String
>> let c = Reflect.construct(String, ['123'], Array); c
String
>> Object.getPrototypeOf(c) === Array.prototype
true
>> c.toSource()
"["1", "2", "3"]"
>> c = Reflect.construct(String, ['1', '2', '3'], Array);
>> // new Array('1', '2', '3') ??
>> c[1]
>> '2'
>> c.constructor
function Array()
>> // Seems that `String` has been totally discarded.
>> // So `newTarget` only makes sense for subclasses to emulate `super()`?