编辑代码

// 1.Constructor 就是 new 时传入的第一个参数,剩余的 arguments 是其他的参数
// 2.使用obj.__proto__ = Constructor.prototype 继承原型上的方法
// 3.将剩余的 arguments 传给 Contructor ,绑定 this 指向为 obj,并执行
// 4.如果构造函数返回的是引用类型,直接返回该引用类型,否则返回 obj

function mynew() {
    const construct = Array.prototype.shift.call(arguments)
    const obj = {}
    obj.__proto__ = construct.prototype
    let res = construct.apply(obj, arguments)
    return obj instanceof Object ? res : obj
}

function mynew1(){
    const construct = arguments[0]
    const obj = {}
    obj.__proto__ = construct.prototype
    let res = construct.apply(obj,[...arguments].slice(1))
    return obj instanceof Object?res:obj
}