functionobjectFactory(){let newObj =null;// 新建一个空对象let constructor =Array.prototype.shift.call(arguments);let result =null;// 判断参数是否是一个函数if(typeof constructor !=='function'){
console.error('type error');return;}// 对象的原型为构造函数的 prototype 对象
newObj = Object.create(constructor.prototype);// 将 this 指向新建对象,并执行函数
result =constructor.apply(newObj, arguments);// 判断返回对象let flag = result &&(typeof result ==='object'||typeof result ==='function');// 判断返回结果return flag ? result : newObj;}
new操作符的实现原理new操作符的执行过程首先创建一个新的空对象;设置原型,将对象的原型设置为函数的prototype对象;让函数的 this 指向这个对象,执行构造函数的代码(为这个新对象添加属性);判断函数的返回值类型,如果是值类型,返回创建的对象。如果是引用类型,就返回这个引用类型的对象;具体实现function objectFactory() { let newObj = null; // 新建一个空对象 let constructor = Array.prototype.s