
在调用new操作符的时候会发生以下四件事
function objectFactory(){
let object=null;
//shift() 方法移除数组的第一项,并返回该值
//在这里是获得接收到的第一个参数
let constructor=Array.prototype.shift.call(arguments);
let result=null;
//判断参数是否是一个函数
if(typeof constructor!=='function'){
console.error("type error");
return;
}
//新建一个空对象,对象的原型为构造函数的prototype对象
newObject =Object.create(constructor.prototype);
//将this指向新建对象,并执行函数
result=constructor.apply(newObject,arguments);
//判断返回对象
let flag=result&&(type of result=='object'||typeof result==='function');
return flag?result:newObject;
}
//使用方法
objectFactory(构造函数,初始化参数);