// 声明构造函数
function Person(name) {
this.name = name
this.sayMyName = function () {
console.log(`你好,我是${this.name}`)
}
}
let hong = new Person('小红')
console.log(hong)
hong.sayMyName()
function myNew(fn, ...args) {
// 1:创建对象
let obj = {}
// 2:将该对象链接至构造函数的原型对象
obj.__proto__ = fn.prototype
// 3:修改this指向
let result = fn.apply(obj, args)
// 4:返回
if (result) return result
else return obj
}
let ming = myNew(Person, '小明')
console.log(ming)
ming.sayMyName()
详情可移步MDN New运算符查看