理论描述
- 创建一个空对象
{}
- 获取构造函数
- 链接到原型
- 绑定this,执行构造函数
- 返回新对象
代码实现
function myNew(){
// 1.创建一个新对象
let obj = {}
// 2.获取构造函数
let con = arguments.__proto__.constructor
// 3.链接原型
obj.__proto__ = con.prototype
// 4.绑定this,执行构造函数
let res = con.aplly(obj , arguments)
// 5.返回新对象
return typeof res === 'object' ? res : obj
}
对于箭头函数来说,它没有自己的prototype
,所以无法链接原型