仿写new运算符

实现new运算符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function myNew(constructor,...arg){
let obj = {};//创建对象
constructor.call(obj,...arg)//改变this执行
obj.__proto__==constructor.prototype//构造函数的原型赋给对象的原型
return obj
}

function Tab(){
this.name = "张三"
this.hobby = function(){
console.log("hobby...")
}
}

let tab1 = myNew(Tab)
console.log(tab1.name)
tab1.hobby()
文章作者: Joker
文章链接: https://qytayh.github.io/2021/01/%E4%BB%BF%E5%86%99new%E8%BF%90%E7%AE%97%E7%AC%A6/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Joker's Blog