function myNew(){
// 1.创建新对象
let newObj = {};
// 2.获取构造函数
let Constructor = [].shift.call(arguments)
// 3.原型挂载
newObj.__proto__ = Constructor.prototype
// 4. 改变this指向到新对象
Constructor.apply(newObj, arguments)
// 5. 返回新对象(还得需要判断返回值)
return newObj
}
const G = function(name){
this.name = name
}
let n = myNew(G,'liwu')
console.log(n.name)
JavaScript手写一个new
最新推荐文章于 2024-03-27 10:59:53 发布