SOURCE

// 创建一个新的对象
// 添加属性
// 指定原型对象
// 如果构造函数没有返回对象,就返回新建的对象

function Person(name, age) {
    this.name = name
    this.age = age

    // return { name: 1 }
}

const p1 = new Person('Tom', 23)

console.log(p1)

function myNew(target) {
    // 新建一个对象
    const obj = Object.create({})

    // 添加属性
    const result = target.apply(obj, Array.prototype.slice.call(arguments, 1))

    // 指定原型对象
    // Object.setPrototypeOf(obj, target.prototype)
    // or
    obj.__proto__ = target.prototype
    
    // 如果构造函数没有返回对象,就返回新建的对象
    return result instanceof Object ? result : obj
}

const p2 = myNew(Person, 'Skill', 23)

console.log(p2)

// 不使用 obj.__proto__ = Object.create(target.prototype) 绑定原型对象
// 是因为会多出一层原型链
console 命令行工具 X clear

                    
>
console