1. // 实现思路
    2. // 1. 创建一个空的对象
    3. // 2. 将函数的arguments的值指向到数组的shift方法上 取出第一个数
    4. // 3. 判断是不是一个函数
    5. // 4. 设置原型 将对象的原型设置为函数的prototye对象
    6. // 5. 判断函数的返回值类型,如果是值类型 返回创建的对象,如果是引用类型就返回这个引用类型的对象
    7. function _new() {
    8. // 取出第一个传入的参数
    9. // shift会改变原来的数组
    10. let constructor = Array.prototype.shift.call(arguments)
    11. // 判断是不是一个函数
    12. if(typeof constructor !='function'){
    13. console.error('Not Function')
    14. }
    15. // 创建一个空的对象
    16. let newObject = Object.create(constructor.prototype)
    17. // let newObject = {}
    18. // newObject.__proto__ = constructor.prorotype
    19. let result = constructor.apply(newObject,arguments)
    20. let flag = result && (typeof constructor =='object' || typeof constructor == 'function')
    21. return flag?result:newObject
    22. }
    23. function fn() {
    24. this.a = arguments[0]
    25. }
    26. console.log(_new(Object, 'zs'));