function isObject(values) {
const val = typeof values;
return values !== null && (val === 'function' || val === 'object')
}
function New(constructorFn, ...args) {
// 创建一个对象,将原型(__proto__)指向传入的参数的原型对象(prototype)
let newObj = Object.create(constructorFn.prototype);
// 把constructorFn的this指向newObj
const result = constructorFn.apply(newObj, args);
return isObject(result) ? result : newObj;
}
function Computer(brand, money) {
this.brand = brand;
this.money = money
}
const c = New(Computer, 'Apple', '120000');
console.log(c); // => Computer { brand: 'Apple' }
console