JS: Reflect.construct
(new in ECMAScript 2015)
Reflect.construct(Fcon, [args])-
same as
new Fcon(args).Fcon is a Constructor, args are arguments.
🟢 TIP: This function is similar to new (operator) but in a function form. Function form lets you manipulate expressions at run-time.
console.log(Reflect.construct(Date, [])); // 2026-02-07T18:12:17.671Z console.log(Reflect.construct(Array, [3])); // [ <3 empty items> ] console.log(Reflect.construct(Map, [[[3, "n3"], [4, "n4"]]])); // Map(2) { 3 => "n3", 4 => "n4" } Reflect.construct(Fcon, [args], Fcon2)-
- Use constructor Fcon to create an object with args, but use constructor Fcon2 to set the parent.
// prototype object const proto1 = Object.create(Object.prototype); proto1.kk = "this is proto1"; // constructor function C1(x) { this.name = x; } C1.prototype = proto1; // s------------------------------ // prototype object const proto2 = Object.create(Object.prototype); proto2.kk = "this is proto2"; // constructor function C2(x) { this.name = x; } C2.prototype = proto2; // s------------------------------ // create a object using constructor C1, but set prototype via constructor C2 const xj = Reflect.construct(C1, ["aa"], C2); console.log(xj); // { name: "aa" } console.log(Reflect.getPrototypeOf(xj) === proto2); // true