new
function new(father ...args){
// 1. 创建一个空对象
let obj = {};
// 2. 设置空对象的原型, 继承于构造函数的原型
Object.setProperty(obj, father.prototype);
// 3. 调用构造函数,将this指向空对象,拿到返回值
let res = father.apply(obj, args);
// 4. 判断返回值是否是一个对象,如果是则返回,如果不是则返回新建的对象
return typeof res === "object" ? res : obj;
}
手撕代码系列