源代码
function mynew(func, ...args) {
// 创建一个空对象,new的结果会得到一个对象
const obj = {}
// 将其原型设置为构造函数的原型
obj.__proto__ = func.prototype
// 1.执行构造函数
// 2.将 this 绑定到构造函数中,并且传递参数
// 3. 得到执行构造函数后的结果
const result = func.apply(obj, args)
// 1.如果构造函数返回的result,是对象则返回该返回值
// 2. 如果构造函数的结果是基本类型,则返回一开始创建的obj
return result instanceof Object ? result : obj;
}