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()
|