目录
- 手写
手写
/*
* @param {Function} fn 构造函数
* @return {*}
**/
function myNew(fn, ...args){
if(typeof fn !== 'function'){
return new TypeError('fn must be a function')
}
// 先创建一个对象
let obj = Object.create(fn.prototype)
// 通过apply让this指向obj, 并调用执行构造函数
let res = fn.apply(obj, args)
return (res instanceof Object) ? res : obj
}
检验:















![Java输入-a,-b,geek,-c,888,-d,[hello,world]字符之后,如何将[hello,world]这个不分开](https://img-blog.csdnimg.cn/26d77fd1a6254f3f8ea9991cfe8eb1d6.png#pic_center)




