Implement myNew(Ctor, ...args)
Write myNew(Ctor, ...args) that replicates what the new keyword does:
- Create a fresh object linked to
Ctor.prototype. - Call
Ctorwiththisbound to that object and the given args. - If
Ctorexplicitly returns an object (or function), use that instead; otherwise use the object created in step 1.
function Point(x, y) { this.x = x; this.y = y; }
myNew(Point, 3, 4); // { x: 3, y: 4 }, instanceof Point