1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| function myNew(fn, ...args) { const obj = {} if (fn.prototype !== null) { Object.setPrototypeOf(obj, fn.prototype); } const result = fn.apply(obj, args);
return result instanceof Object ? result : obj; }
function Person(name, age) { this.name = name; this.age = age; }
const person = myNew(Person, 'Jack', 12); console.log(person);
|